Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Select constructor at runtime

Posted on 2003-02-26
4
Medium Priority
?
133 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
  • 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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…

580 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