Re: Create objects using dynamic class names
- From: "Boefje" <boefje@xxxxxxxxxxx>
- Date: 11 Aug 2006 04:11:16 -0700
Hi Maarten,
Thanks for your reply.
I already have got a base class. So maybe I can use your suggested
sollution.
I understand that my code looks suspicious. Maybe it is less suspicious
when I explain what I am trying to achieve.
I am trying to make a Wizard form. The idea is to build it in such a
way it will be easy to use it in my other projects. It must be as
general as possible, but without overdoing it.
So I have got a form. This form has got two buttons, a 'previous'
button and a 'next' button. I would like to 'say' to the form that I
would like to use 6 steps. The form constructs the 6 frames which all
have a comon ancestor. (This ancestor has properties like StepNumber
and GoBackAllowed).
What I would like the form to do is create these frames at runtime. If
I click 'next', the form creates the next frame. If the next step is 4,
I want it to create an instance of TFrameStep4. The idea is that the
frame holds the specific code for step 4.
Now I come back to your proposed sollution. You say: "A more elegant
solution to your current problem could be to derive your TFrameStapX
classes from a common base class and have an array of class references
to the individual classes."
Would that be an open array? And can I create a class name
(TFrameStapX) on the fly? Do I understand it right that I can call the
constructor of the base class, which will create an instance of a sub
class, based on the constructor's parameters?
So I say something like this
lFrameStap4 := TFrameStap(PanelMidden, 4)
and I end up with lFrameStap4 being of type TFrameStap4? Did I
understand it right? Can you show me a little example? Or maybe my
total approach is wrong. I would be happy to hear it.
Thanks for your time,
B.
Maarten Wiltink schreef:
"Boefje" <boefje@xxxxxxxxxxx> wrote in message
news:1155285374.570852.177570@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
The subject might be an incorrect description of what I want. What I
want is quite simple.
I have got a part of code whic looks like this:
lFrame := TFrameStap1.Create(PanelMidden, 1);
FFrameStappen.Add(lFrame);
lFrame := TFrameStap2.Create(PanelMidden, 2);
FFrameStappen.Add(lFrame);
lFrame := TFrameStap3.Create(PanelMidden, 3);
FFrameStappen.Add(lFrame);
lFrame := TFrameStap4.Create(PanelMidden, 4);
FFrameStappen.Add(lFrame);
lFrame := TFrameStap5.Create(PanelMidden, 5);
FFrameStappen.Add(lFrame);
Because I keep repeating myself, I would do this in some kind of loop.
for i:= 0 to MAXFRAMES - 1 do
begin
lFrame := XXXXXXXXXXXXXXX.Create(PanelMidden, i);
FFrameStappen.Add(lFrame);
end;
With what should I replace XXXXXXXXXXXXXXX? If it is possible, I guess
I have to do something with ClassName. Is it possible?
Yes. There are at least three different ways, and more variants that
may qualify as different depending on how you look at it.
The most literal solution is to use GetClass or FindClass. The help
file knows.
But that's not the best solution. A more elegant solution to your
current problem could be to derive your TFrameStapX classes from
a common base class and have an array of class references to the
individual classes. Usually, this requires a virtual constructor so
you can use 'the same' constructor each time, yet end up executing
code that is appropriate to the current subclass.
This replaces the repetition of code with a table and a loop. You
still have to hardcode the table. That's a common pattern.
But your code looks suspicious. It has the same running number in
_two_ places. Doesn't TFrameStap4 already know that it's number 4?
Logic dictates that you could either do without the final parameter,
or merge the five classes into one.
Groetjes,
Maarten Wiltink
.
- Follow-Ups:
- Re: Create objects using dynamic class names
- From: Maarten Wiltink
- Re: Create objects using dynamic class names
- From: Hans-Peter Diettrich
- Re: Create objects using dynamic class names
- References:
- Create objects using dynamic class names
- From: Boefje
- Re: Create objects using dynamic class names
- From: Maarten Wiltink
- Create objects using dynamic class names
- Prev by Date: Re: Foreign chars in window title bar
- Next by Date: Re: Create objects using dynamic class names
- Previous by thread: Re: Create objects using dynamic class names
- Next by thread: Re: Create objects using dynamic class names
- Index(es):