[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to return a class when I have the class name as a string

Posted on 2004-11-11
7
Medium Priority
?
314 Views
Last Modified: 2010-04-05

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
0
Comment
Question by:johan777
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 6

Assisted Solution

by:pritaeas
pritaeas earned 800 total points
ID: 12553140
You can use FindClass() to find the corresponding class.
0
 
LVL 6

Assisted Solution

by:pritaeas
pritaeas earned 800 total points
ID: 12553149
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
 
LVL 27

Assisted Solution

by:kretzschmar
kretzschmar earned 400 total points
ID: 12553853
getclass would be also a possibility
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 800 total points
ID: 12554440
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
 

Author Comment

by:johan777
ID: 12562879
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
 

Author Comment

by:johan777
ID: 12562890
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
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12563669
> 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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Screencast - Getting to Know the Pipeline
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month17 days, 21 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question