Try OpenEdge Now
skip to main content
Object-oriented Programming
Programming with Class-based Objects : Instantiating and managing class-based objects : Dynamically accessing a property at runtime
 

Dynamically accessing a property at runtime

You can access a class property dynamically using the DYNAMIC-PROPERTY function or statement by providing a character expression for the property name, along with an object reference, THIS-OBJECT system reference, or a class type. The compiler allows the object reference to be of any user-defined type or .NET type.
At runtime, the AVM performs the following:
*Determines the actual type of the object reference
*Resolves the property name expression
*Validates and performs the assignment.
The following syntax is for the DYNAMIC-PROPERTY function:

Syntax

[data-element = ] DYNAMIC PROPERTY ( {object-reference |class-type-name}
  , property-name[ , index] )
The following syntax is for the DYNAMIC-PROPERTY statement. Through the DYNAMIC-PROPERTY statement you can set a property’s value without knowing the type of the property’s name at compile time:
DYNAMIC PROPERTY ( {object-reference |class-type-name} , property-name
  [ , index] ) = new-value
data-element
Specifies an ABL data element which is assigned a value returned when you invoke the class property's GET accessor. The appropriate CAST or conversion function is applied to convert the property’s value to the data type of data-element.
object-reference
Specifies a reference to an ABL or .NET class instance that exposes the specified property as an instance member. The compiler allows object-reference to be declared as any object type. At runtime, the object type must resolve to the type that exposes the property.
class-type-name
Specifies the name of an ABL or .NET class type that defines the specified property as a static member. This is a CHARACTER expression that the AVM evaluates to the type name of a class at runtime. It must only be class type names: interfaces are not valid.
property-name
Specifies a CHARACTER expression that evaluates to the property name at runtime.
index
Specifies an integer value for the index of the specified element. Use index to set or retrieve an individual array element. You can also operate on an entire array by removing the index similar to working with non-dynamic property access.
new-value
An expression of any data type. Its value is converted at run time to the property’s data type.
ABL also provides the GetPropertyValue( ) and SetPropertyValue( ) methods with functionalities similar to the DYNAMIC PROPERTY function. Use the GetPropertyValue( ) and SetPropertyValue( ) methods to access and set a property’s value at runtime, when you do not know the property’s name or type at compile-time. There is a difference between using GetPropertyValue( ) or SetPropertyValue( ) and using DYNAMIC PROPERTY: Using GetPropertyValue( ) or SetPropertyValue( )requires an instance of Progress.Lang.Class in addition to the object reference when accessing instance properties.
Conversions between the property type and the source type of a SET or the target type of a GET succeed as long as the conversion between the data type of the source and the data type of the target of the assignment is supported. The AVM does the required conversions automatically as if there were a CAST or an ABL conversion function such as STRING or INTEGER used.
The following code snippets compare the dynamic and non-dynamic ways of accessing a property.The following ABL code snippet shows non-dynamic access of the property:
DEF VAR objref AS Invoice.
objref = NEW Invoice().
objref:Amount = 654.32. /* set the Amount property */

MESSAGE objref:Amount. /* get the Amount property */
In the example above, 654.32 is set as the value for Amount property at compile-time.
The following code uses DYNAMIC-PROPERTY:
DEF VAR objref AS Invoice.
objref = NEW Invoice().

/* set the Amount property */
DYNAMIC-PROPERTY(objref, "Amount") = 654.32.

/* get the Amount property */
MESSAGE DYNAMIC-PROPERTY(objref, "Amount").
In the example above, setting and getting values for the Amount property is resolved dynamically through the DYNAMIC-PROPERTY statement.
The following code snippet demonstrates the use of Progress.Lang.Class’s SetPropertyValue( ) and GetPropertyValue( )methods:
DEF VAR objref AS Invoice.objref = NEW Invoice().
DEF VAR classref as Progress.Lang.Class.
classref = objref:GetType().

/* set the Amount property */
classref:SetPropertyValue (objref, "Amount", 654.32).

/* get the Amount property */
MESSAGE classref:GetPropertyValue(objref, "Amount").
In the example above, setting and getting values for the Amount property is resolved at runtime through the SetPropertyValue( ) and GetPropertyValue( )methods.