[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

Creating an object at runtime

I have to create an object at runtime.
The Name of the class is stored in a stringlist and I have to create my objects out of this list.

Here a piece of pseude code :

  string[0] = 'TBitmap'
  string[1] = 'TButton'
  string[2] = 'TEdit'
  string[3] = ''

Somewhere later I would like to do the
following :

var
  obj: TObject;
  nCnt: INteger;
begin

  nCnt = 0
  while string[nCnt]
  begin
     obj = string[nCnt].Create;

This is the task, who can give me the syntax ???

0
GerhardTreisbach
Asked:
GerhardTreisbach
1 Solution
 
kretzschmarCommented:
hi gerhard,

just a guess,
and really not tested
...
nCnt = 0
  while string[nCnt]
  begin
     obj = GetClass(string[nCnt]).Create;

...
maybe you must register your needed classes first

meikl
0
 
King_DavidCommented:
procedure TForm1.CreateControlStr(Str: String);
var
  Control: TControl;
  ControlClass: TControlClass;
begin
  ControlClass := TControlClass(GetClass(Str));
  Control := ControlClass.Create(Application);
  Control.Parent := Self;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  RegisterClasses([TButton]);
  CreateControlStr('TButton');
end;
0
 
GerhardTreisbachAuthor Commented:
Thanx King David,
that's exactly what I'm looking for.

Nevertheless thanx to meikl, the idea was the right one, but King Davids solution is ready coded and so he get the points.

So long
   Gerhard
0
 
lfgmartinsCommented:
Hi,
How can I do a  registerclass(TForm1), with a the name passed from a string var ('TForm1'). I need this to alllow dynamic form names. What else I'd have to do a registerclass([TUser, TContracts,...]) form my 50 forms.
Thanks,
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Tackle projects and never again get stuck behind a technical roadblock.
Join Now