Delphi, VirtualStringTree - classes (objects) instead of records
Asked Answered
O

4

4

I need to use a class instead of record for VirtualStringTree node.

Should I declare it standard (but in this case - tricky) way like that:

PNode = ^TNode;
TNode = record
 obj: TMyObject;
end;
//..
var
 fNd: PNode;
begin
fNd:= vstTree.getNodeData(vstTree.AddChild(nil));
fNd.obj:= TMyObject.Create; 
//..

or should I use directly TMyObject? If so - how?! How about assigning (constructing) the object and freeing it?

Thanks in advance m.

Oilskin answered 18/2, 2010 at 12:14 Comment(0)
S
8
  1. Setup datasize for holding object

    vstTree.NodeDataSize := SizeOf(TMyObject); 
    
  2. Get the datasize holder and bind to your object

    vstTree.getNodeData(passed in interested node)^ := your object
    

    or

    vstTree.getNodeData(vstTree.AddChild(nil))^ := TMyObject.Create;
    

    or
    use vstTree.InsertNode method

  3. To free the binding object hookup the OnFreeNode event

    vstTree.OnFreeNode := FreeNodeMethod;
    

    with

    procedure TFoo.FreeNodeMethod(Sender: TBaseVirtualTree; Node: PVirtualNode);
    var
      P: ^TMyObject;
    begin
      P := Sender.getNodeData(Node);
      if P <> nil then
      begin
          P^.Free;
          P^ := nil; //for your safety or you can omit this line
      end;
    end;
    
Schnorrer answered 18/2, 2010 at 22:21 Comment(0)
Y
1

you could create the object instance after receiving the node data, as in :

fNd:= vstTree.getNodeData(vstTree.AddChild(nil)); 
fnd.obj := TMbyObject.Create;

or you could try and assign it directly

Pointer(Obj) := vstTree.getNodeData(...);

Ytterbite answered 18/2, 2010 at 20:1 Comment(0)
E
1

And you can free your object in OnFreeNode event.

Elbrus answered 18/2, 2010 at 21:53 Comment(1)
This should be a comment to Aldo's answer.Marilynnmarimba
M
1

Just add an object reference to your record. Use the OnInitNode and OnFreeNode events to create and destroy your object.

Marilynnmarimba answered 18/2, 2010 at 22:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.