?
Solved

Select constructor at runtime

Posted on 2003-02-26
4
Medium Priority
?
132 Views
Last Modified: 2010-04-04
Hi experts,

I have got something like this:
type
  TBase = class...
  TMyClass_1 = class(TBase)...
  TMyClass_2 = class(TBase)...
  ...
  TMyClass_n = class(TBase)...
var
  myVar: TBase;
  ...
  myVar := TMyClass_m.Create;   // m in [1..n]

Now I want to store the class type in a file (maybe the name of the class?) and next time I run the application read the type and call the proper constructor. Of course I can do it with some if/then/else stuff but I want something more maintainable (array of classtype etc.). If neccessary I will increase points.

Reagrds
J
0
Comment
Question by:j42
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 204 total points
ID: 8024038
something like

with TBaseClass(GetClass(BaseClassName)).Create(self) do
...
   
whereas

type TBaseClass = Class Of TBase;

BaseClassName is a StringVariable which holds like 'TMyClass_1'

you must also register all subclassed object
procedure RegisterClasses([...]);
best placed in the initialization-section

hope thats it

meikl ;-)
0
 
LVL 2

Author Comment

by:j42
ID: 8024313
First of all I'd like to thank you for your immediate reply!

There are some things I fell not so comfortable with:
- TBase has to be derrived from TPersistent
- If I add new derrived classes, I've to remember to register :-/ (so it's the same as my if/then/else bunch)
I guess theres no way to avoid this?

Regards
J
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8024382
you can't simple create unkown objects,
thats why they are must be registered
0
 
LVL 2

Author Comment

by:j42
ID: 8024532
Thanks you very much!
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

770 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