Often when you add a field or property you need to add code to the class constructor or destructor to initialize or finalize the member.
The Creational Wizard is used to centralize this process and define initialization / finalization code while you add the member.
Optionally a parameter is added to the constructor parameter list to initialize a member. You may select an existing constructor or create a new one.
Similar finalization snippets can be inserted in a new or existing destructor.
MMX Code Explorer includes this wizard.
Pascal Example: adding a field with constructor / destructor code
Assume you have a class TDocument and want to add a field FLines: TStrings to store the contents like this:
…then usually you need to add a constructor with code that instantiates FLines and add a destructor with code to dispose the lines again, for example like this:
1 2 3 4 5 6 7 8 9 10 11 |
constructor TDocument.Create; begin inherited Create; FLines := TStringList.Create; end; destructor TDocument.Destroy; begin FreeAndNil(FLines); inherited Destroy; end; |
This is exactly what the wizard does! Go to the Creational Wizard Tab, and click the “Composition” preset:
This preset selects a constructor and destructor and suggests code snippets, based on the field type and name.
after clicking OK, not only the field is inserted, but also the constructor, destructor and suggested code snippets.
Exactly as we wanted it! Instead of creating a new constructor, you can add the snippets to an existing constructor.
1 2 3 4 5 6 7 8 9 10 11 |
constructor TDocument.Create; begin inherited Create; FLines := TStringList.Create; end; destructor TDocument.Destroy; begin FreeAndNil(FLines); inherited Destroy; end; |
Pascal Example: initializing multiple fields and properties at once.
If you added a more fields and properties to your Document class, for example Header, Footer and Parent that all need
initialization code, you can invoke the Creational Wizard on the class itself.
This class wizard is similar to the one on the field and property dialogs, except that it shows all
fields and properties in a class and a preset can be applied to multiple selected members at once.
… as you see in the above dialog, Header, Footer and Lines are initialized and destroyed, the Parent document is an aggregate reference.
Clicking OK results in the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
constructor TDocument.Create(AParent: TDocument); begin inherited Create; Parent := AParent; FHeader := TStringList.Create; FFooter := TStringList.Create; FLines := TStringList.Create; end; destructor TDocument.Destroy; begin FreeAndNil(FLines); FreeAndNil(FFooter); FreeAndNil(FHeader); inherited Destroy; end; |
Customizing the wizard
The wizard is customizable using a type based look-up list in file CreationalWizard.txt in the shared directory,
for example “C:\Program Files\ModelMakerTools\Shared”. Here you define type initialization and finalization code per language.