See Also: Properties, Web Properties, Struct Properties, Array Properties, Class, Get, Object, Set
To create properties in class and object definitions.
Property {type} {name} [{initial-value}]
Property {type} {name} public|private [{initial-value}]
For Web Applications, you should use Web Properties for any properties that can be changed on the client.
You may define properties of classes and objects with the property command. Each property must have a defined data type, which may be string, number, date, integer, or real.
Each property must have an initialValue that is assigned whenever an object of the class is created. The values of properties may be changed with the set command, and queried with the get command.
When created in an object, a property's definition must be placed directly within the object and not within a procedure or function.
Object MyEdit is a cDbTextEdit
Property Integer piMyVal 0
:
End_Object
When created in a class, a property's definition must be placed within a function or procedure, usually Construct_Object.
Class cMyEdit is a cDbTextEdit
Procedure Construct_Object
Forward Send Construct_Object
Property Integer piMyVal 0
:
End_Procedure
:
End_Object
Each property's value may be accessed by either the get command or through the use of an expression. The name of the message is the name of the property.
Object oMyEdit is an cMyEdit
Set piMyVal to 10
Procedure AddOne
Integer iVal
Get piMyVal to iVal
Increment iVal
Set piMyVal to iVal
// the expression syntax to access this property is:
// Move (piMyVal(self)) to iVal
End_Procedure
:
End_Object
// the property is accessed outside of the object as follows
Procedure DoIt
Integer iVal
Get piMyVal of oMyEdit to iVal
Increment iVal
Set piMyVal of oMyEdit to iVal
// the expression syntax to access this property is:
// Move (piMyVal(oMyEdit)) to iVal
End_Procedure
Data types are maintained by the property. If a set is done from, or a get done to, a different data type, then automatic type conversion is performed.
There is no limit to the number of properties that an object or class may have. Properties are inherited, so an object that handles a complex process may have many data properties to manage itself.
Properties may also be delegated to. A child of an object may access the properties of its parent without specifically addressing the parent as the destination of the messages.
Often a Procedure Set and Function will be created using the same name allowing the developer to access the methods as a "get/set" pair. Read more about Properties in the Language Guide.
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.
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
Special consideration needs to be given for accessing data that is stored in array properties. The Get and Set commands that are used for accessing a property's data can only be used to get and set the entire array. This works just like an array variable assignment.
To read or write individual array property elements, it is necessary to get the property to a local array variable of the same type. You would then manipulate the data of this local array and write back the changes by setting the array property once again.
Class cMyButton is a Button
Procedure Construct_Object
Forward Send Construct_Object
// Declare an array property
Property String[] psNames
End_Procedure
Procedure StoreName String sNewName
String[] sNames // create a local array variable
Integer iElementCount
// get the array property to the variable.
Get psNames to sNames
// Add the new name.
Move (SizeOfArray(sNames)) to iElementCount
Move sNewName to sNames[iElementCount]
// Save the change back to the array property.
Set psNames to sNames
End_Procedure
End_Class
By default, properties are public and may addressed by their name. A property can be defined as private by adding the keyword private after its name. This is a technique that is no longer encouraged, but is one you may see in existing DataFlex applications.
Class cMyEdit is an Edit
Procedure Construct_Object
Forward Send Construct_Object
Property Integer piMyVal Private 0
:
End_Procedure
Procedure DoThis
Integer iMyVal
// you must add the class name to access a private property
Get cMyEdit.piMyVal to iMyVal
End_Procedure
:
End_Object
If the property is private, it may be accessed by the object's class name followed by a period and the property name. Normally a private property should only be accessed within the class that defined it.