Mega Code Archive

 
Categories / Delphi / Ide Indy
 

How to Re Initialize an Object to its Default State (Delphi)

Title: How to Re-Initialize an Object to its Default State (Delphi)? In Delphi OOP, a constructor is used to create an instance of a class. The constructor is, in general, called "Create". When you define your own classes (deriving from TObject) with properties and methods (and events) you mostly always initialize values in the constructor. Initializing values means assigning default values to properties. While an object (instance of a class) lives, property values get changed. Here's how to simply re-initialize an object to default (property) values: For example, a custom TFilter class can have 2 public fields: "Year" and "Language". You might want to intialize Year to '2006' and Language to 'Delphi': type TFilter = class(TObject) Year : integer; Language : string; constructor Create; end; implementation constructor TFilter.Create; begin Year := 2006; Language := 'Delphi'; end; If you have a variable "filter" of type "TFilter", to create an instance you call the "TFilter.Create" method on the class. The Create method (constructor) here acts as a class function/procedure. To re-initialize the "filter" object you need to call "Create" on the object itself: var filter :TFilter; begin filter := TFilter.Create; try //initialized data Edit1.Text := Format('Year: %d, Lang: %s',[filter.year, filter.Language]) ; //changing data filter.year := 1996; filter.Language := 'Pascal'; Edit2.Text := Format('Year: %d, Lang: %s',[filter.year, filter.Language]) ; //re-initialize filter.Create; Edit3.Text := Format('Year: %d, Lang: %s',[filter.year, filter.Language]) ; finally filter.Free; end; end; When the filter variable is initialy created, by calling TFilter.Create, the constructor initialize the fields Year to '2006', Language to 'Delphi'. We display the values in the Edit1 box, We then assign different values to both Year and Langauge, display the new values in Edit2, To re-initialize to default values, we call Create on the instance variable. That's it :) Daniel Wischnewski comments: Create is missing to call the inherited constructor, which will, in a real OOP-design, together with inheritance (reserving memory, initializing relevant objects, etc) lead to problems. If you insert the inherited create, a new object will be created, thus leading to memory hogs ;-) Zarko: If a class inherits directly from TObject - there's no need for "inherited" in the constructor - as TObject.Create is empty. Next, this code will *not* compile in a .NET environment, as you are not calling the inherited constructor in the Create-method. Zarko: Yep, I do agree :( Why not simply create a method for re-initializing the required fields and make it public. This would be the easiest of all ways and one does not need to call the "Create" method on an already created object. Zarko: Yes, in most cases that would be better. This tip serves the purpose of showing how a class method (Create) can be called on an instance object. If a class has only few native type properties and inherits directly from TObject, this type of re-initializing an object is handly.