I want to use Treenodes with my own objects and have declared them like this:
MyObject1 = class(TTreeNode)
aValue1 : integer;
Public
constructor Create(Owner: TTreeNodes);
procedure .......
end;
MyObject2 = class(MyObject1)
aValue2 : integer;
public
constructor Create(Owner: TTreeNodes);
procedure ......
end;
In the TreeView.OnCreateNodeClass
event, I set:
NodeClass := MyObject2;
When I then create a new node in my tree with:
TreeNode := TreeView1.Items.AddChild(ParentNode, '');
I believed that MyObject2.Create
would be called, but it is not. The MyObject2
is created as it should, so I can access the fields aValue1
and aValue2
when the node is created, but I would like to initialize them in my constructor.
Anyone have any idea about this?
CodePudding user response:
You need to add override
to each of your constructors to ensure that they will be called. You must also make sure that you call inherited
in each constructor implementation.
The
MyObject2
is created as it should so I can access the fieldsaValue1
andaValue2
when the node is created but I would like to initialize them in my constructor.
I don't think that is true. I think that when you try to access those fields you will just be accessing some random part of memory that is outside the actual node instance.