Structures revisited!

Structures offers enormous flexibility when you design a program. They allow to store information in a nice clean way into your variables. I like to explain structures as a single record database. A structure is like a series of fields that can be stored in one record (the variable). Take the following example:

struct (s$, "a", "b");

The variable s$ has two elements, a and b. Each element can contain a separate value. The default type for structure's elements is a TINT (4 bytes) value. You can also specify which value type the element will be holding.You have multiple choices here:

TBYTE 1 byte
TSHORT 2 bytes
TINT 4 bytes
TUINT 4 bytes (unsigned)
TWIDE 4 bytes (unicode character string)
TDOUBLE 8 bytes
TLONG 8 bytes (no decimal)

struct (s$, "a", tbyte, "b", tdouble);

The following structure s$ would be 9 bytes in size. 1 byte for element a and 8 bytes for element b. If you need to specify your own size in bytes you can also easily do it:

struct (s$, "a", tbyte, "b", 50);

Element b contains 50 bytes. Now how do you access the structure variable elements you ask? Noting is easier:

s.a$ = 10;
s.b$ = 20;

What about strings in structures? You will see that PPL is very flexible but can also be a little more complex to use in some cases. You will need to be careful when using strings in structures. PPL either stores a pointer of the string that is assigned to the structure's element in the case where the element size is TINT, TUINT, TWIDE, TDOUBLE or TLONG. If the element size is a user-defined length, then the string is copied directly to the structure's element memory location.

struct (s$, "a", "b", 50);
s.a$ = "Hello World!";
s.b$ = "Hello Again!";

The main difference here is that the string "Hello World!" is not stored in s.a$ but rather stored somewhere in memory and only its pointer address is stored in s.a$. "Hello Again!" is stored directly into s.b$.

ShowMessage(s.a$);

If we try to access s.a$ like the previous code, only its pointer address value will be printed. To access s.a$ as a string we need to use the @ operator to convert a pointer to a string.

ShowMessage(@s.a$);

Now "Hello World!" will be printed in the dialog message. To access s.b$ no need to do anything special.

ShowMessage(s.b$);

This will display "Hello Again!".