johan777
asked on
How to return a class when I have the class name as a string
Hi all,
Please help! I have a scenario in my app where I have the name of a class (as a string) and I need to pass the class as a parameter.
Below is an similar example. What needs to be done to resolve this?
For example:
var
AClassStringName: string;
begin
AClassStringName := 'TLabel';
CreateControl(?????AClassS
end;
Regards,
Johan Swart
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
last line should read .. I'm going to split the points if you all don't mind as the first three comments was correct, but Alex explained it the best. ;-)
> even if I type cast it, I can still pick up th original true class type inside the procedure
Yep, that's the magic behind the virtual methods of classes. Since the Create() method of TComponent is a virtual method, it will search internally for the correct Create() method for your class instead of just using the TComponent.Create() method.
Yep, that's the magic behind the virtual methods of classes. Since the Create() method of TComponent is a virtual method, it will search internally for the correct Create() method for your class instead of just using the TComponent.Create() method.
ASKER
Thanx all for the excellent help!!!
I have tried GetClass first-time, but as Alex pointed out, it's returns is TPersistent. Fortunately all the classes I require are all derived from one common class. Thus, I type casted it to the class and viola! it worked.
Alex, thanx for the explanation. What I found was that even if I type cast it, I can still pick up th original true class type inside the procedure so it seems to work and I did not have to write a lot of code. Setting of the properties is handled in a different way which does not leave me with a huge if statement.
I'm going to split the points if you all don't mind as the first two comments was correct, but Alex explained it the best.
Regards,
Johan Swart