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(?????AClassStringName????, 'ANameForTheObject', 10, 10, 100, 200);

end;


Regards,
Johan Swart
johan777Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pritaeasSoftware EngineerCommented:
You can use FindClass() to find the corresponding class.
0
pritaeasSoftware EngineerCommented:
http://www.obsof.com/delphi_tips/delphi_tips.html

See here for an example and tips. About half way down is an example. Look for FindClass.
0
kretzschmarCommented:
getclass would be also a possibility
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Wim ten BrinkSelf-employed developerCommented:
GetClass or FindClass will work only on classes that are registered for the streaming mechanism or explicitly registered by calling RegisterClass or RegisterClasses. It can only create classes that are inherited from TPersistent and even worse, you need to know which constructor you need to call. TPersistent has no class-related constructor but inherits it from TObject. But any TComponent-based class will require the TComponent-related constructor to be called or the object won't be created in the correct way.
This means that after you've converted the name to a class type, you then have to use this code:

var
  YourClassType: TPersistentClass;
  AComponent: TComponent;
begin
  YourClassType := FindClass( 'TLabel' );
  if ( YourClassType.InheritsFrom( TComponent ) ) then begin
    AComponent := TComponent( YourClassType ).Create( nil );
  end;
end;

And remember, since your code still doesn't know what class type it actually is, you will have lots of problems handling all other properties of the class. In above code, I just check if it's TComponent-compatible and if it is, I typecast the class to TComponent. Since it's a label, I might want to set other properties too but I just don't know which they are. So replacing TComponent with TLabel would help a lot. But if you have to check for 50 or more class types, it will become quite a complex piece of code...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
johan777Author Commented:
Wow!

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
0
johan777Author Commented:
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. ;-)
0
Wim ten BrinkSelf-employed developerCommented:
> 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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.