Properties in Classes and Objects
Properties are the variables of object-oriented programming. In procedural DataFlex, you cannot write a very sophisticated program without creating variables. In object-oriented DataFlex, you cannot write very sophisticated programs without defining new properties. Properties can be defined within a class or defined within an object.
It is easy to define properties inside a class. Properties must be defined inside your construct_object procedure. If we had a subclass that needed two properties, we would define them as follows:
Class Vndr_Data_dictionary1 is a Vndr_DataDictionary
procedure construct_object
forward send construct_object
property boolean pbActiveOnly true
property string psValidStat ''
end_procedure
end_class
// Here's how we would use this class
Object Vndr_DD is a Vndr_DataDictionary1
set psValidStat to "A"
Procedure OnConstrain
String sValidStat
Boolean bActiveOnly
Get psValidStat to sValidStat
Get pbActiveOnly to bActiveOnly
constrain Vndr.status eq sValidStat
if (bActiveOnly) ;
constrain Vndr.Active_stat eq "Y"
end_Procedure
end_object
If you only need to define properties within a single object, you can define the properties directly in the object. Defining a property inside an object has the same effect as placing a procedure or function inside an object; it creates an object class. The following example will create the same two properties directly inside an object class. Properties in an object should be placed near the top of the object and must not be placed inside a function or procedure.
Object Vndr_DD is a Vndr_DataDictionary
// Define new properties
Property boolean pbActiveOnly True
Property string psValid_Stat 'A'
Procedure OnConstrain
String sValidStat
Boolean bActiveOnly
Get psValidStat to sValidStat
Get pbActiveOnly to bActiveOnly
constrain Vndr.status eq sValidStat
if (bActiveOnly) ;
constrain Vndr.Active_stat eq "Y"
end_Procedure
end_object
Do not use global variables to store data that is local to objects; use properties.
Delegation and Properties
It is possible to get and set the value of any property through delegation. This means that a property that is defined in an object can be accessed by any of the object's children, grandchildren, great-grandchildren, etc. This is a very powerful concept. You can use properties to allow objects to communicate. A property value may be set in one object and retrieved in another. The following segment shows how you might use a property and delegation to allow you to create a list that will display either all records or active records.
Object oVndrLookupList is a dbModalPanel
// Define property which determines if filter is used
Property Boolean pbActiveOnly False
// Create DDO with conditional constraint
Object Vndr_DD is a Vndr_DataDictionary
Procedure OnConstrain
Boolean bActiveOnly
// Get value of property through delegation
get pbActiveOnly to bActiveOnly
If (bActiveOnly) ;
constrain Vndr.Stat eq 'A'
end_Procedure
end_object
// Create the DEO with Alt+T as filter toggle
Object C_List is a dbList
set server to Vndr_DD
on_key key_alt+key_t Send Flip_Filter
begin_row
entry_item Vndr.NUMBER
entry_item Vndr.CUSTOMER
entry_item Vndr.Stat
end_row
// Flip the constraint
Procedure Flip_Filter
integer bActiveOnly
// Get and set this property through delegation
Get pbActiveOnly to bActiveOnly // Get property
Set pbActiveOnly to (not(bActiveOnly)) // Flip its value
// Rebuild the constraints w/ the new value
Send rebuild_constraints of Vndr_DD
Send beginning_of_data // Redisplay the list
end_procedure
end_object
end_object