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 ???

GerhardTreisbachAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.