Hi all,
I thought I knew how this worked, but recently encountered something that threw me. Could you please help me understand what's going on?
The sample code below creates a fresh copy of an existing object.
In this example, only the CheckBox1.Checked = True;
I can fix this by changing the line that creates the b object to
With this change, both checkboxes are checked. But I don't understand why.
Thanks for your help.
I thought I knew how this worked, but recently encountered something that threw me. Could you please help me understand what's going on?
The sample code below creates a fresh copy of an existing object.
Code:
[b]type[/b]
TBaseObject = [b]class[/b]
[b]private[/b]
FFlag : Boolean;
[b]public[/b]
[b]constructor[/b] Create; [b]virtual[/b];
[b]end[/b];
TMyObject = [b]class[/b](TBaseObject)
[b]public[/b]
[b]constructor[/b] Create; [b]override[/b];
[b]end[/b];
TMyClass = [b]class[/b] [b]of[/b] TBaseObject;
[b]constructor[/b] TBaseObject.Create;
[b]begin[/b]
[b]inherited[/b];
[b]end[/b];
[b]constructor[/b] TMyObject.Create;
[b]begin[/b]
[b]inherited[/b];
FFlag := True;
[b]end[/b];
[b]procedure[/b] TForm1.Button1Click(Sender: TObject);
[b]var[/b]
a : TBaseObject;
b : TObject;
[b]begin[/b]
a := TMyObject.Create;
b := a.ClassType.Create; [navy][i]// a.ClassType = TMyObject[/i][/navy]
[b]try[/b]
CheckBox1.Checked := a.FFlag;
CheckBox2.Checked := TBaseObject(b).FFlag;
[b]finally[/b]
b.Free;
a.Free;
[b]end[/b];
[b]end[/b];
In this example, only the CheckBox1.Checked = True;
I can fix this by changing the line that creates the b object to
Code:
b := TMyClass(a.ClassType).Create;
With this change, both checkboxes are checked. But I don't understand why.
Thanks for your help.