Struct Properties

Special consideration needs to be given for accessing data that is stored in class properties that are of struct type. The Get and Set commands that are used for accessing a property's data can only be used to get and set the entire struct. This works just like a struct variable assignment.

To read or write individual members of a struct property, it is necessary to get the property to a local struct variable of the same type. You would then manipulate the data of this local struct and write back the changes by setting the struct property once again.

Example…

struct tNameAndAddress

    string sName

    string sAddress

end_struct

 

Class cMyButton is a Button

 

    Procedure Construct_Object

        Forward Send Construct_Object

        // Declare a struct property

        Property tNameAndAddress pContact

    End_Procedure

 

    Procedure StoreName String sName

        tNameAndAddress contact    // create a local struct variable

        

        // get the struct property to the variable.

        Get pContact to contact

        

        // show the current name.

        Send Info_Box ("The current name is: " + contact.sName)

        // change the name.

        Move sName to contact.sName

        // Save the change back to the struct property.

        Set pContact to contact

    End_Procedure

 

End_Class

Accessing struct properties utilizes the copy-on-write concept, which means that getting/setting struct properties do not impose a full copy operation itself. If you are only reading the struct members in a procedure, then the copy to a local struct variable is very fast. DataFlex will only copy a reference to the struct until the first time that you perform a write to this copy. At the time of the first write a full copy will be made. Thus if your code never writes to the copy, then it will be very efficient.

For more information refer to Setting Property Values and Reading Property Values.