| View previous topic :: View next topic |
| Author |
Message |
R. Vermeij Guest
|
Posted: Wed Jan 14, 2004 11:24 am Post subject: Re: Cast a Class by Name |
|
|
This is not possible.
Maybe you can do this by some sort of factory.
TMyFormFactory = class(TObject)
public
procedure RegisterMyClass(const AName: string; AClass: TMyClass);
function GetClassByName(const AName: string): TMyClass;
end;
This is just a rough idea. (Only public members shown).
Internally you have to keep a list of registered classed.
MyFormFactory: TMyFormFactory;
....
MyFormFactory.RegisterMyClass('MyName', TMyDescendant);
....
AClass := MyFormFactory.GetClassByName('MyName');
Ruud
|
|
| Back to top |
|
 |
Humberto Zilio Guest
|
Posted: Wed Jan 14, 2004 11:55 am Post subject: Cast a Class by Name |
|
|
I want to know if is possible cast a Class by the name.
Like this
TBasic
| Quote: | _ TBomb
_ TBombModel
|
I want to pass to another formulary an object of type TBasic.
And I want to call a formulary based in the name of the class instanced.
Like:
var
Objeto: TBasic;
Objeto := TBomb.Create;
ClassByName('Tfrm'+Objeto.ClassName).Execute;
Objeto := TBombModel.Create;
ClassByName('Tfrm'+Objeto.ClassName).Execute;
This is possible?
Thanks.
Humberto Zilio
|
|
| Back to top |
|
 |
Joanna Carter (TeamB) Guest
|
Posted: Wed Jan 14, 2004 12:41 pm Post subject: Re: Cast a Class by Name |
|
|
Humberto Zilio wrote:
| Quote: | I want to know if is possible cast a Class by the name.
This is possible?
|
Have you looked at the Class Factory Pattern? All you need to do is to
create a Singleton class that holds a list of classes and has a method that
returns an instance for the name passed in.
type
TFormFactory = class
public
class procedure RegisterFormClass(AClass: TClass);
class function CreateForm(AClassName: string): TBasic;
end;
implementation
var
_Classes: TClassList;
class procedure TFormFactory.RegisterFormClass(AClass: TClass)
begin
_Classes.Add(AClass);
end;
class function TFormFactory.CreateForm(AClassName: string): TBasic;
var
i: Integer;
begin
i := 0;
Result := nil;
while (i < _Classes.Count) and Result = nil do
begin
if _Classes[i].ClassNameIs(AClassName) then
Result := _Classes[i]
else
Inc(i);
end;
end;
initialization
_Classes := TClassList.Create;
finalization
_Classes.Free;
end.
==============
Use it like this:
begin
TFormFactory.RegisterFormClass(TBomb);
TFormFactory.RegisterFormClass(TBombModel);
...
TFormFactory.CreateForm(TBomb).Execute;
end;
Joanna
--
Joanna Carter (TeamB)
Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker
|
|
| Back to top |
|
 |
Marc Rohloff Guest
|
Posted: Wed Jan 14, 2004 1:50 pm Post subject: Re: Cast a Class by Name |
|
|
Humberto Zilio wrote on Wed, 14 Jan 2004 09:55:53 -0200 ...
There are two alternatives that I can think of to the factory pattern:
1) Give the base class a class method to return the correct form.
type TBasic = class
...
class function GetForm:TForm; virtual; abstract;
end;
and override it in your subclasses. THen you can use:
Objeto := TBomb.Create;
Objecto.GetForm.Execute;
2) A second option would be to use RegisterClass and FindClass.
RegisterClass(TfrmBomb);
RegisterClass(TfrmBombModel);
....
Objeto := TBomb.Create;
FormName := 'Tfrm'+copy(Objeto.Classname,2,length(Objecto.Classname));
Form := TFormClass( FindClass(FormName) ).Create(Application);
Form.Execute;
Marc Rohloff
marc rohloff at bigfoot dot com
|
|
| Back to top |
|
 |
Alan Guest
|
Posted: Mon May 17, 2004 2:00 am Post subject: Re: Cast a Class by Name |
|
|
| Quote: | Use it like this:
begin
TFormFactory.RegisterFormClass(TBomb);
TFormFactory.RegisterFormClass(TBombModel);
...
TFormFactory.CreateForm(TBomb).Execute;
end;
|
Do I need to declare a global varible for this TFormFactory ?
Or Delphi will store this TFormFactory somewhere ?
|
|
| Back to top |
|
 |
David Clegg Guest
|
Posted: Mon May 17, 2004 2:18 am Post subject: Re: Cast a Class by Name |
|
|
Alan wrote:
| Quote: | Do I need to declare a global varible for this TFormFactory ?
Or Delphi will store this TFormFactory somewhere ?
|
In the example that Joanna cited there is no need for an instance
variable at all. All the operations were being done via class methods.
These relate to the class as a whole, rather than an instance of that
class, and are not accessed via a class instance.
--
Cheers,
David Clegg
dclegg_at_ebetonline_dot_com
Vote 1 http://cc.borland.com/codecentral/ccweb.exe/listing?id=21489
Now supports Google Groups searching with Dyna-extend(tm) technology!
Quality Central. The best way to bug Borland about bugs.
http://qc.borland.com
"Note to self. Stop doing anything." - Homer Simpson
|
|
| Back to top |
|
 |
|