Solved

Register class at run time

Posted on 2012-04-05
11
2,002 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
  • 5
  • 4
11 Comments
 
LVL 15

Author Comment

by:mikelittlewood
Comment Utility
Forgot to say Delphi 2006 version
0
 
LVL 36

Expert Comment

by:Geert Gruwez
Comment Utility
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
Comment Utility
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
 
LVL 36

Expert Comment

by:Geert Gruwez
Comment Utility
oh yeah, descendants of TDummy would work too :)
and descendants of those descendants ... and so on
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 15

Author Comment

by:mikelittlewood
Comment Utility
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 36

Expert Comment

by:Geert Gruwez
Comment Utility
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
Comment Utility
Possibly, but isn't that 2009 onwards only? I'm only on 2006.
0
 
LVL 36

Expert Comment

by:Geert Gruwez
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now