Select constructor at runtime

Hi experts,

I have got something like this:
  TBase = class...
  TMyClass_1 = class(TBase)...
  TMyClass_2 = class(TBase)...
  TMyClass_n = class(TBase)...
  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.

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.

something like

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

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 ;-)

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
j42Author Commented:
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?

you can't simple create unkown objects,
thats why they are must be registered
j42Author Commented:
Thanks you very much!
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

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.