Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Register class at run time

Posted on 2012-04-05
11
Medium Priority
?
2,161 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

721 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