How to create an instance of a class by class name in a string?

How to create an instance of a class by class name in a string?

Post by Marc Youn » Sat, 18 Oct 2003 15:07:02


I have an application that has one base class and several descendant classes (13classes and the class name in a String. What I am looking for is a way to say..
var
S:String;
Begin
S:='TMyClass';
ClassByName(S).Create;
End;

The string will actually be loaded from a database and could be any of the thirtdefine classes that could be used in this manner as well, so my application coul
I would like to avoid a big mess like

If S='TMyClass1' Then
TClass1.Create
Else If S='TMyClass2' Then
TClass2.Create
...

Thanks!
 
 
 

How to create an instance of a class by class name in a string?

Post by Terry Russ » Sat, 18 Oct 2003 17:16:43


classes (13 ?classes and the class name in a String. What I >am looking
for is a way to say.. ?
thirt ?define classes that could be used in this manner as well, so my
application coul ?

Whats is messy about that? You do this sort of type mapping often,
find a way to automate the code or table generation.

Find an editor with string generation macros
loop with substitution, a few seconds.

Unless they are some registered type or somehow existing
so you can match typenames you don't have much choice
determining name->type mapping.



type tcbase=class of TMyBase;

var descendantclass:tcbase;
var descendant:TMybase;

descendantclass:=Tmybase;
S:='T1';
If S='T1' Then descendantclass:=T1
else If S='T2' Then descendantclass:=T2;

if not (descendantclass = TMybase)
then
descendant:=descendantclass.Create;

 
 
 

How to create an instance of a class by class name in a string?

Post by Maarten Wi » Sat, 18 Oct 2003 17:36:59


RegisterClass and GetClass can do this for you. Do you have static or
virtual constructors, and why?

Groetjes,
Maarten Wiltink
 
 
 

How to create an instance of a class by class name in a string?

Post by erewho » Sat, 18 Oct 2003 18:45:32

Surely this is a matter of turning the problem upside down

You have one 'base' class and 13 descendants

Why not have 1 Class and 13 'variations' ?

On Fri, 17 Oct 2003 06:07:02 -0000, Marc Young < XXXX@XXXXX.COM >
 
 
 

How to create an instance of a class by class name in a string?

Post by Bruce Robe » Sun, 19 Oct 2003 01:16:02


The classes have to descend from tPersistent. There is also FindClass,
unlike GetClass it raises an exception if the class isn't registered.
 
 
 

How to create an instance of a class by class name in a string?

Post by Dave Lange » Sun, 19 Oct 2003 01:50:25

> I have an application that has one base class and several descendant classes (13 ?classes and the class name in a String. What I am looking for is a way to say.. ?

Maybe you could implement a 'class of' type of construction? An example
is given on:

http://www.yqcomputer.com/
("Class-reference types" in Delphi's help)

I wouldn't know how to manage this without any 'case'/'if-then'
constructs if the type of class initially is represented as a String
type, but maybe you can rewrite things a bit so that you have different
input.

--
M.vr.gr.
Dave L.
(d-dot-langers-at-wxs-dot-nl)
 
 
 

How to create an instance of a class by class name in a string?

Post by Marc Youn » Sun, 19 Oct 2003 10:24:46


In <<3GUjb.4455$ XXXX@XXXXX.COM >>, "Bruce Roberts" <ber@bouncei>



Thanks for the responses!

The classes descend from TObject. I would like to stay away from the TPersistenthousands of objects into memory so access is very fast. I could make my own clThat would also allow me to write plug-in dlls that have registered types as weland static methods from a usage standpoint, an ancestor that calls a virtual metdifferentiate at create time I have to call the correct constructor to begin wit
Thanks.
 
 
 

How to create an instance of a class by class name in a string?

Post by Maarten Wi » Mon, 20 Oct 2003 00:54:39


[...]

That looks horrible, and is completely unintelligible. Please get a
decent newsreader and get your encodings fixed, better yet, scrapped.

Groetjes,
Maarten Wiltink
 
 
 

How to create an instance of a class by class name in a string?

Post by Richard At » Tue, 21 Oct 2003 08:52:38


Dave Langers < XXXX@XXXXX.COM > had the generosity to contribute:

This sounds a lot like an Abstract Factory pattern.

If you look at how RegisterClass, etc, work in Classes.pas it's just a look
up of a list of classes, relying on tobject.classname to do the look up. You
could always bastardize this code for your own purposes. The frustration I
find with RegisterClass is that there is no way of enumerating the
registered classes as the list is an implementation global. I would like to
check that I remembered to register everything that I intended to without
having to wait for the run-time error.

FWIW
Richard