Solved

Register class at run time

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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