Board index » delphi » Return a list of Derived Classes

Return a list of Derived Classes

Hey

    I would like to create object at runtime. The problem is that I don't
know the type at design time. And also I don't know how many classes
(actually I do know, but I am pretending that I don't) are derived from base
class.

--------------------------------------------------------------------
Structure:
TDevices (collection of TDevice)

TDevice  --> TDev1
               --> TDev2
               ....
               --> TDevN
--------------------------------------------------------------------

I would like to load 'ClassName' of all classes derived form TDevice into
ComboBox. And than at RunTime select a 'ClassName' from ComboBox, Create the
object of selected type and Add it to the collection (TDevices).

Thanks

btw:
Any suggestions on how to Create an object if you have its ClassName
(string)

 

Re:Return a list of Derived Classes


"Damjan" <damjan.veng...@kompozitemra.si> skrev i melding
news:j3Ef9.1584$EM5.58805@news.siol.net...

Quote
> Hey

>     I would like to create object at runtime. The problem is that I don't
> know the type at design time. And also I don't know how many classes
> (actually I do know, but I am pretending that I don't) are derived from
base
> class.

> --------------------------------------------------------------------
> Structure:
> TDevices (collection of TDevice)

> TDevice  --> TDev1
>                --> TDev2
>                ....
>                --> TDevN
> --------------------------------------------------------------------

> I would like to load 'ClassName' of all classes derived form TDevice into
> ComboBox. And than at RunTime select a 'ClassName' from ComboBox, Create
the
> object of selected type and Add it to the collection (TDevices).

> Thanks

> btw:
> Any suggestions on how to Create an object if you have its ClassName
> (string)

Look at the TReader code (classes.pas), and see how it's done there.

--
Bj?rge S?ther
bjorge@hahaha_itte.no

Re:Return a list of Derived Classes


Quote
"Damjan" <damjan.veng...@kompozitemra.si> wrote in message news:j3Ef9.1584
> I would like to load 'ClassName' of all classes derived form TDevice into
> ComboBox. And than at RunTime select a 'ClassName' from ComboBox, Create
the
> object of selected type and Add it to the collection (TDevices).

The list of registerd classes is declared and maintained in the
Implementation section of one of the VCL units (I can't remember which one
off-hand). Without implementing your own mechanism for tracking descendants
its going to be quite difficult. I'd suggest that you implement a
RegisterDescendant<classname> procedure that maintains a list of descendant
classes:

type

tSomeClassClass = class of tSomeClass;

procedure RegisterSomeClassDescendant (aClass : tSomeClassClass);
procedure GetSomeClassDescendantNames (results : tStrings);

Implementation

var SomeClassDescendants : tList = tSomeClassClass;

procedure RegisterSomeClassDescendant (aClass : tSomeClassClass);

begin
if not Assigned (SomeClassDescendants)
then SomeClassDescendants := tList.Create;
if SomeClassDescendants.IndexOf (aClass) < 0
then SomeClassDescendants.Add (aClass);
end;

procedure GetSomeClassDescendantNames (results : tStrings);

var i : integer;

begin
results.BeginUpdate;
try
    results.Clear;
    if Assigned (SomeClassDescendants)
    then begin
        for i := 0 to (SomeClassDescendants.Count - 1) do
            results.Add (tObject (SomeClassDescendants [i]).ClassName;
        end;
finally
    results.EndUpdate;
    end;
end;

Finalization

if Assigned (SomeClassDescendants)
then SomeClassDescendants.Free;

Other Threads