4D v16

Generic programming (without pointers)

Home

 
4D v16
Generic programming (without pointers)

Generic programming (without pointers)    


 

 

In addition to the manipulations explained in the video, you should also note that:

These exercises are set up to show one specific way doing things.

Depending on your own habits and the standards of your company, you may use different ways of organizing, of naming fields and variables, of establishing an order for passing parameters, and so on.
If you feel that another way is more suitable, try it out and compare its advantages and drawbacks. That way you can rely on your own experience.

A method can receive up to 32 parameters (from $1 to $32). However, it is unusual to use more than 7 or 8 parameters.

You should make liberal use of generic programming.

The next section covers the use of pointers, which complements what we will see here.

Generic programming also lets you reduce the length of your code by concentrating a series of functions within the same method where you pass several parameters.
To set up generic programming correctly, here are a few observations:

  • As soon as you write several lines of codes that are similar to previous ones, there is probably a generic solution that you can implement.
  • (Later on) when you hesitate to create a generic method: go ahead and do it, chances are you won't regret it!
  • Not everything can be generic. Sometimes trying to make things too generic may lead to a loss of productivity (code not easy to read, difficult to maintain, takes up too much memory, increased processing time, and so on),

In this video, we're going to learn how to call methods and use parameters.

We'll cover pointer type parameters later on.

The purpose of generic programming is to optimize development and to facilitate maintenance and portability.

We've already used a simple call to a method (without parameters) in the "On Startup" database method where we called the NAVIGATION method.

In the first programming lessons, we saw how to configure buttons according to the page selected in the navigation form.

Now we're going to optimize this programming so as to keep only one method that is used for all the navigation buttons (All, Query, Order by, and so on).

  • Let's display the Explorer
  • We're going to create a new method
  • A project method that we will name NAVIGATION_FUNCTIONS.

It will receive 2 parameters:

  • The first concerns the action to perform in the form of text (Search, Order by, etc.)
  • The second corresponds to the page where the click occurs (1, 2, 3 and so on)

We call this method as follows:

In a button, we will call NAVIGATION_FUNCTIONS

  • The text corresponding to the action that we want to perform
  • and here, as the 2nd parameter, is the page concerned.

NAVIGATION_FUNCTIONS("All";FORM Get current page)
So we want to use the navigation functions concerning the "All" item and passing the number of the page to process as the 2nd parameter.

There are two ways to type the parameters of the method that is called, in other words, the NAVIGATION_FUNCTIONS method:

  • The 1st parameter will be a text: C_TEXT($1)
  • The 2nd will be a longint: C_LONGINT($2)

You'll notice that to name parameters, we use the syntax $1, $2, ... $n to retrieve each parameter.

To make your method easy to read, we suggest that you create local variables with explicit names where you assign the value of the parameters received. So it is better to write:

 C_TEXT($1;$Action)

since we can declare several variables in a compiler directive of the C_TEXT type for simple variables.
Here is the 2nd variable which will be the page number:

 C_LONGINT($2;$PageNum)

and to retrieve the value of $1  in $Action and the value of $2 in $PageNumber, you can just write:

 $Action:=$1
 $PageNumber:=$2

The rest of the method is an adaptation of what we already described in previous sections; namely, adjusting the action to the request and configuring it according to the page and thus the table chosen:

We will use:

  • a Case of

:($Action="All")

  • and here we will retrieve the code of the "All" button
  • Cut
  • and adapt it in the method.

What about the information we need to replace?

  • The FORM Get current page is no longer needed since it is sent by the calling method. We'll replace it by $PageNumber
  • For the moment, we don't need to change the rest.

When we cover pointers, then we'll replace what we have here with a more generic method.

We can do the same thing with the Query button.

  • Here we drag the code by holding down ALT to copy it
  • Then we'll have an "All" action and a "Query" action
  • We'll copy the code from the "All" button
  • Paste it into the "Query" button
  • Replace the action here
  • Copy the code from the "Query" button
  • And go back to the method here

and, as we did before, replace FORM Get current page by the variable that we received.

We're going to put a break point and check that we go to this method when the form is executed.

If we click on "All", we see the method being executed but what we're specifically interested in is the stacking of the methods:

  • the last one called is at the top
  • the previous one is on the bottom.

If we double-click on this method, the code appears that leads us to this point and we can see that the NAVIGATIONS_FUNCTIONS method was called (with the parameters: "All", FORM Get current page). The method was received. Here it received the parameters: $1 = "All", $2 = the page number--here it is page 1.

Next, $Action is "All" so we're supposed to go to this part
Then $PageNumber is 1, so we will use ALL RECORDS on the Interventions table to determine the number of records concerned.

We will then:

  • Remove the break point
  • Execute the method

When we get to Technician, if we search for those whose name contains "bon", we will find BONAPARTE.
and if we request "All"

We can see that our method works correctly.

 
 

 
PROPERTIES 

Product: 4D
Theme: Generic programming (without pointers)

 
HISTORY 

 
ARTICLE USAGE

Self-training ( 4D v16)