KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Writing methods that can modify their parameters
PRODUCT: 4D | VERSION: | PLATFORM:
Published On: May 7, 1999

You may have noticed that 4D's built-in Methods can do some things that your own methods cannot. One such thing you might like to emulate is that a built-in Method can modify the parameters that are passed to it. The Process Properties command is a good example: it sets values in up to four variables, effectively returning multiple values in one step.

Strictly speaking, you can't get your 4D Methods to do this, because the parameters your 4D method receives must be kept in local variables ($1, $2 and so on).

However, you can write your method to accept pointers to variables, fields and so on in these local variables. Your method can then modify the contents of the things the pointers reference.

Here is a simple example. You pass it a string as its first parameter, and pointers to two other variables or fields as the other two parameters. It converts the string passed to it to upper case and lower case, and puts these into the two variables or fields:

c_text($1)
c_pointer($2;$3)
$2->:=Uppercase($1)
$3:=Lowercase($1)

To call this method (let's call it "StringIt"), use pointers to variables or fields as its second and third parameter, like this:

c_text(a;b)
StringIt("ThIS iS mixeD."; ->a; ->b)
alert(a)
alert(b)

This code will display two alerts: "THIS IS MIXED." and "this is mixed."

Note that while pointers are fairly simple to use for applications like this, you should read the section in the Language Reference dealing with pointers, since there are a few constraints you must keep in mind when using them.

It is also a good idea to always check whether a pointer is Nil before trying to use it, particularly if you intend to compile your application.