Re: dynamic object creation
- From: "Bruce Roberts" <dontsendtober@xxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 27 Oct 2005 17:45:50 -0400
"B.r.K.o.N.j.A." <thebrkonja@xxxxxxx> wrote in message
news:djra9f$sd2$1@xxxxxxxxxxxxxxxxx
> Is there a way to do something similar to this?
>
> var classname: string;
>
> classname:='someclass';
>
> myclass:=<class contained in classname variable (someclass)> .create;
>
> I need to end up with myclass variable that holds reference to newly
> created
> someclass object.
If the class descends from tPersistent you'll need something like
var ClassName : string;
ClassType : tPersistentClass;
Instance : tPersistent;
ClassName := 'SomeClass';
try
ClassType := FindClass (ClassName); // or use GetClass
except
ShowMessage ('Class ' + ClassName + ' not found - is it registered?');
end;
Instance := ClassType.Create;
You have to be careful doing this. If your class descends from a descendant
of tPersistent that overrides the constructor, say tComponent, you need to
do something like
var ClassType : tComponentClass;
Instance : tComponent;
.. . .
ClassType := tComponentClass (FindClass (ClassName));
.. . .
Instance := ClassType.Create (someOwner);
When working with Instance, you will also have to use typecasts to get to
various methods and properties. Unless, of course you declare Instance of
an appropriate type and do the typecast on construction.
Finally, any class you create this way still has to be declared and defined
in your code. You may also need to Register the class.
.
- References:
- dynamic object creation
- From: B.r.K.o.N.j.A.
- dynamic object creation
- Prev by Date: Re: @ operator ? Or maybe not
- Next by Date: Re: dynamic object creation
- Previous by thread: dynamic object creation
- Next by thread: Re: dynamic object creation
- Index(es):