BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Cast a Class by Name

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design
View previous topic :: View next topic  
Author Message
R. Vermeij
Guest





PostPosted: Wed Jan 14, 2004 11:24 am    Post subject: Re: Cast a Class by Name Reply with quote




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





PostPosted: Wed Jan 14, 2004 11:55 am    Post subject: Cast a Class by Name Reply with quote



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





PostPosted: Wed Jan 14, 2004 12:41 pm    Post subject: Re: Cast a Class by Name Reply with quote



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





PostPosted: Wed Jan 14, 2004 1:50 pm    Post subject: Re: Cast a Class by Name Reply with quote

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





PostPosted: Mon May 17, 2004 2:00 am    Post subject: Re: Cast a Class by Name Reply with quote

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





PostPosted: Mon May 17, 2004 2:18 am    Post subject: Re: Cast a Class by Name Reply with quote

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 Smile
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
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.