Thankyou Ben for the explanation/example.
That is an interesting piece of code.
A few observations/questions:

1.
f.GetClasses(CallbackClass);

I cannot find information about TClassFinder or TClassFinder.GetClasses anywhere.
Where would one find this information? (I assume the return type for the function getClasses is TClassList)


Following the code in the debugger, the line above appears to be a pipe-like construct that serializes the output from getClasses. Is this a correct interpretation?
(coming from a Jade background I would have expected the call syntax to be callbackClass(f.GetClasses) with a for loop in callbackClass method to iterate the passed collection).


2.
It is perhaps unfortunate that Delphi requires class registration? This appears to negate the purpose- addition of new subclasses will require added code to register them (for a small number of classes it would be simpler to manually add them to TSubclassList).


thanks again, John.


From: Ben Taylor <[EMAIL PROTECTED]>
Reply-To: NZ Borland Developers Group - Delphi List <[EMAIL PROTECTED]>
To: NZ Borland Developers Group - Delphi List <[EMAIL PROTECTED]>
Subject: Re: [DUG] How do you determine the subclasses of a given class at runtime?
Date: Fri, 23 Jan 2004 14:19:11 -0800 (PST)


hi,

this should help :-)

  TSubclassList = class(TClassList)
  private
    FParentClass:TPersistentClass;
    procedure CallbackClass(AClass: TPersistentClass);
  public
    procedure Execute(const aClass:TPersistentClass);
  end;

procedure TSubclassList.CallbackClass(AClass: TPersistentClass);
begin
 if AClass.InheritsFrom(FParentClass) and (AClass<>FParentClass) then
  begin
  Self.Add(AClass);
  end;
end;

procedure TSubclassList.Execute(const aClass: TPersistentClass);
var
 f:TClassFinder;
begin
 FParentClass:=aClass;

 f:=TClassFinder.Create(nil);
 try
 f.GetClasses(CallbackClass);
 finally
 FreeAndNil(f);
 end;
end;

-------------------------------------

example use:

  TFruit = class(TPersistent);
  TOrange = class(TFruit);
  TBigOrange = class(TOrange);
  TApple = class(TFruit);

procedure TForm1.FormCreate(Sender: TObject);
var
l:TSubclassList;
i:Integer;
begin
 l:=TSubclassList.Create;
 l.Execute(TFruit);
 for i:=0 to l.Count-1 do
 begin
 ListBox1.Items.Add(l[i].ClassName);
 end;
 freeandnil(l);
end;

initialization
RegisterClass(TFruit);
RegisterClass(TOrange);
RegisterClass(TBigOrange);
RegisterClass(TApple);

cya,
ben

--- john rae <[EMAIL PROTECTED]> wrote:
> Hi. I would like to be able to generate class references to the subclasses
> of a given class so that I can populate a list with sub class class specific
> information and then upon user selection, use a soft coded factory method to
> instantiate the corresponding subclass. This means new subclasses can be
> added and everything will still work...
> Function TObject.parent supports upward navigation, how do I navigate down?
>
> thanks, John.



__________________________________ Do you Yahoo!? Yahoo! SiteBuilder - Free web site building tool. Try it! http://webhosting.yahoo.com/ps/sb/ _______________________________________________ Delphi mailing list [EMAIL PROTECTED] http://ns3.123.co.nz/mailman/listinfo/delphi

_________________________________________________________________
Need more speed? Get Xtra Jetstream @ http://www.xtra.co.nz/products/0,,5803,00.html !


_______________________________________________
Delphi mailing list
[EMAIL PROTECTED]
http://ns3.123.co.nz/mailman/listinfo/delphi

Reply via email to