4D v16

Pointers

Home

 
4D v16
Pointers

Pointers    


 

 

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

Using a pointer is pretty easy. The advantage of its use is to make programming even more generic and adaptable.

The concept of a pointer is a simple one that we use everyday in speaking.

In the sentence “my dog is at home”, we use two pointers or indirections.

In fact, “my dog” doesn’t define the name or breed of the dog. You would need to know who is speaking to define what dog we’re talking about. That also goes for the house.

If Paul says “put it in the drawer” - what drawer is he talking about?

The drawer Paul indicated with his finger. He pointed to a drawer that the other person will “de-point” (look towards which drawer Paul’s finger is pointing).

This simple notion works well in 4D and allows replacing drawers with tables, fields, or variables.

If I ask 4D to perform a query in this table, I first need to define which table to query by pointing to it as follows:

 MyPointer:=->[TABLE]

At first, you may have a little trouble with pointers but you will soon find that they are simple to set up and provide you with a considerable amount of power.
Pointers are often used as parameters for methods that you call. We recommend that you spend some time familiarizing yourself with their use because they are sure to come in handy whenever you find yourself thinking:

  • "I'm going to duplicate this button"
  • or "I need to copy this method and then adapt it".

When you have to manage lists of pointers, then you can look into pointer arrays.

Remember that you can also use the Get pointer command that lets you set the name of pointers based on a character string. For example:

 Get pointer("MyVariable"+String($i))

In this video, we're going to learn about pointers and how to optimize their programming and make it generic.

As we saw in the previous exercise, setting options for each action (query, order by, etc.) in the NAVIGATION_FUNCTIONS  method can quickly become tedious. So, we need to set the action towards the table corresponding to the selected page.

In that case, it’s easier to indicate table to be used just once at the beginning of the method. We can store this directive in a variable and then use this variable in the commands.

Logically speaking, we can’t say that a variable is equal to a table; these objects are completely different and have completely different content as well. We’ve already learned that a variable can be an integer, number, text, picture, etc. It contains a value of the relevant type. A table isn’t “content” like a numeric value is.

The solution is to use a variable of the pointer type.

So we're going to set this variable, which is a local variable:

 C_POINTER($TablePointer)

This variable exists in memory. Now to assign a value, we'll use the following type of syntax:

 $TablePointer:=->[INTERVENTIONS]

So as we saw for variables, we have:

  • initialization
  • assignment of value

and, later on, wherever the Interventions table should have been used, we'll be able to replace it with this variable, that we'll need to "de-point" in this case.

The advantage being that all the code is here, a simple copy-paste with only a change of table, can be replaced by these 2 same lines of code, provided that we have previously set the table to where it must point depending on the page number.

So we have to get the code that we wrote earlier and here we indicate that:

  • If we are on page 1, we point to the interventions (->[INTERVENTIONS])
  • If we are on page 2, we point to the technicians (->[TECHNICIANS])

That said, we no longer need to test the pages here and so it's no longer useful to have this duplicate program.

We have a 1st program here that lets us:

  • set the table concerned depending on the page
  • and then set the action to perform regardless of the page since the correct table was set.

The same principle applies for searches:

  • we'll move the code here
  • completely remove the "Case of" that is here
  • and replace "intervention" with a pointer to the table since we are on page 2

When we get here, the pointer will be a pointer to "Technicians" and so we will search in the "Technicians" table.

The number of records found is the number of records found in the "Technicians" table.

 
 

 
PROPERTIES 

Product: 4D
Theme: Pointers

 
HISTORY 

 
ARTICLE USAGE

Self-training ( 4D v16)