Create a class instance dynamically
- Last Updated: January 16, 2024
- 1 minute read
- OpenEdge
- Version 12.8
- Documentation
Suppose a Corporation class has been written to create and access class instances dynamically. The Corporation class has defined a temp-table, ttBusinessUnit, with information about all the possible classes that the application may instantiate at runtime.
define private temp-table ttBusinessUnit no-undo
field MaxNumDepartments as character
field ClassName as character // .CLS name
field BusinessUnitRef as Progress.Lang.Object.
When the application starts, records are added to the ttBusinessUnit temp-table for every class that could be used at runtime for all use cases of the application. At runtime, only a subset of the possible classes is instantiated, based upon the use case. Here is the code to dynamically create the required class instances that are now known at runtime. In this example, the classes that are instantiated have a single argument constructor for the name of the business unit.
// add code here to dynamically create the business unit depending
// on the Type value
if ttBusinessUnit.Type = "Company"
then
ttBusinessUnit.BusinessUnitRef = dynamic-new(ttBusinessUnit.Classname)
(ttBusinessUnit.Name).
else
ttBusinessUnit.BusinessUnitRef = dynamic-new(ttBusinessUnit.Classname)
(ttBusinessUnit.Name,ttBusinessUnit.MaxNumDepartments).
empty temp-table ttDepartment.