.NET Constructors (Magic xpa 4.x)
A constructor instantiates and initializes an object. Constructors are methods in which the name of the method is the same as the class itself. For example, the StringBuilder object has a constructor named StringBuilder(). There may be more than one constructor, each with a different set of parameters.
Using a constructor is an operation that needs to be performed only once. If this is performed a second time, a new instance of the object is created. As a consequence, you need to take special care as to where to define the constructor.
When you work with a Rich Client task, the Task Prefix cannot accept client-side operations and since the .NET runs on the client, you cannot run a constructor from the Task Prefix (except in Offline tasks where the Task Prefix is a client-side operation). When you consider where to define the constructor, you should be aware of the following:
-
Multiple records – You can run the constructor from the Record Prefix logic unit. However, if there are multiple records and/or when the display mode of the form is set to Line mode, you have to remember to condition the instantiation to be evaluated only once. You can condition it by using the Magic xpa IsFirstRecordCycle(0) function.
-
Recompute – It is not recommended to run a constructor (or any method for that matter) from a property of a .NET control, because Magic xpa performs multiple form refreshing, so the .NET object will be created in each refresh.
For example, when you want to define a font, it is possible to use a constructor method directly from the Font property of the control. However, the constructor will be invoked with every refresh of the form. In this case it is better to create an ad hoc variable that you update with the constructor of the font, and to use that variable in the Font property of the control.