Solved

Register class at run time

Posted on 2012-04-05
11
2,098 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 37

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
Industry Leaders: 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 37

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 37

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 37

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

728 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