Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Dynamic Object Creation

12 views
Skip to first unread message

Chris Birmele

unread,
Mar 8, 1999, 3:00:00 AM3/8/99
to
Hi,

I need the ability to create different objects depending on which
TTreeNode is selected. I don't like to hard code the link between the
TTreeNode and the object to be created, but like to use a file or
dbase :

TreeItem1 = TListView
TreeItem2 = TTreeView
TreeItem3 = TButton

How can I create objects without knowing the type until runtime as in:

AObject :=
(TTypeIsReadFromFile).Create(WhatAboutDifferentConstructors) ???

Appreciate your expertise!

Regards
Chris

Parshin Maxim

unread,
Mar 8, 1999, 3:00:00 AM3/8/99
to
You can use class references like this:

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
type
TClassType = Class of TControl;
var
T:TClassType;
C:TControl;
begin
if RadioGroup1.ItemIndex=0 then
T:=TButton
else
T:=TEdit;
c:=T.Create(self);
C.Parent:=self;
C.Left:=X;
C.Top:=Y;
end;

Medardo Rodriguez

unread,
Mar 8, 1999, 3:00:00 AM3/8/99
to
Well, I propose that you register all posible controls classes you could
use. Create an array of class types and prepare mechanisms to register
classes. A simple aproach could be:

type
TControlClass = class of TControl;

var
ClassRegistry : array[byte] of TControlClass;
ClassCount : integer = 0;
// It's not good to declare fix arrays, but you can modify this

procedure RegisterControlClass(which : TControlClass)
begin
ClassRegistry[ClassCount] := which; // maybe you must check if this
class was registered before
inc(ClassCount);
end;

then you can associate each item with a class name. And when you obtain that
name you can search the class and use it to construct the control object.

function SearchControlClass(const name : string) : TControlClass;
var
i : integer;
begin
i := low(ClassRegistry);
while (i < ClassCount) and (ClassRegistry[i].ClassName <> name) do
inc(i);
if i < ClassCount
then Result := ClassRegistry[i]
else Result := nil;
end;

I did'n test this and neither use the proper structures, but you can see the
point.

Best regards

Med


0 new messages