Solved

Register class at run time

Posted on 2012-04-05
11
2,120 Views
Last Modified: 2012-05-25
Hi,

I'm not sure if I can do this, but I wanted to know if I can derive a new class from a base registered class just by using a string.
I want to load a list of classes into a stringlist, register each one based on TMyBaseObjectClass, so that further on I can use GetClass to instantiate a specific class type.
Hope I've explained this enough, but basically I want to remove the 2 marked lines below, but still be able to use GetClass in DoSomethingString to create objects for use.

uses
  Classes;

type
  TMyBaseObject = class(TPersistent)

  end;
  TMyBaseObjectClass = class of TMyBaseObject;

  TMyNewClass = class(TMyBaseObject); // REMOVE THIS LINE

  procedure DoSomethingClass(pClass: TMyBaseObjectClass);
  procedure DoSomethingString(pClassString: String);

var
  FMyNewObject: TMyBaseObject;

implementation

procedure DoSomethingClass(pClass: TMyBaseObjectClass);
begin
  FMyNewObject := pClass.Create();
end;

procedure DoSomethingString(pClassString: String);
var
  FClass: TMyBaseObjectClass;
begin
  FClass := TMyBaseObjectClass(GetClass('TMyNewClass'));
  FMyNewObject := FClass.Create();
end;

initialization
  RegisterClass(TMyBaseObject);
  RegisterClass(TMyNewClass); // REMOVE THIS LINE

end.
0
Comment
Question by:mikelittlewood
[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
  • 5
  • 4
11 Comments
 
LVL 15

Author Comment

by:mikelittlewood
ID: 37810647
Forgot to say Delphi 2006 version
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 37810706
you  can pass any class derived TMyBaseObject
these are basic principles of OOP  (nice feature of using "class of 'class' ")

type
  TDummy = class(TMyBaseObject)

i don't see why you need to register the class to use it ?
>> why do you need that ?
0
 
LVL 15

Author Comment

by:mikelittlewood
ID: 37810708
Ah I think I can only do this sort of thing from delphi 2010 with all the new RTTI stuff.
I think I need to manually register each class, no way round it as far as I can see.
Or am I wrong?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 37810709
oh yeah, descendants of TDummy would work too :)
and descendants of those descendants ... and so on
0
 
LVL 15

Author Comment

by:mikelittlewood
ID: 37810726
Hey Geert,

I want to be able to use the GetClass function, but you can only do this is you register the class in the first place.
I want, in your example, to create a class called TDummy, without actually having an object defined called TDummy.
I want to take a string, create a class at run time called TDummy which inherits from TMyBaseObject.
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 37811404
aren't you looking for the datasnap technology ?
http://edn.embarcadero.com/article/images/39227/The-New-Datasnap-in-Delphi-2009_WP.pdf

i saw a demo for 2009 of passing "classes" as dataobjects with datasnap technology
they recreated the DBexpress for this
only available since the D2009 version
0
 
LVL 15

Author Comment

by:mikelittlewood
ID: 37811413
Possibly, but isn't that 2009 onwards only? I'm only on 2006.
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 37811577
yep, that's the bad news

what are you trying to do ?
dbexpress datasnap is for client-server system ...
0
 
LVL 15

Accepted Solution

by:
mikelittlewood earned 0 total points
ID: 37811606
Its ok Geert, I don't think I am able to do what I want on the current version I have.
Think I can do this a less elegant way for now, might look at updating the software at a later date.

Thanks anyway
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses
Course of the Month5 days, 3 hours left to enroll

635 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