Solved

How do you create a new instance of a class (or child thereof) when all you have is a source instance (object)?

Posted on 2003-10-30
3
164 Views
Last Modified: 2010-04-05
How do you create a new instance of a class (or child thereof) when all you have is a source instance (object)?
Just to explain what I mean, I've created the example below where I want to copy a TField instance (or child thereof).

function CreateNewFieldInstance(const ASrcField: TField): TField
begin
  Result := ASrcField.Create(self);
  // Wrong unfortunately, because Delphi unlike other languages doesn't create a new instance here,
  // causing Result to end up being the same object as ASrcField.
end;

The code inside the function can't assume the class to create is TField, because it may be TIntegerField or some other derivative.

0
Comment
Question by:craigmanley
  • 2
3 Comments
 

Author Comment

by:craigmanley
ID: 9649411
I found the solution my self. In case of the example I gave it would be:

function CreateNewFieldInstance(const ASrcField: TField): TField;
begin
  Result := TFieldClass(ASrcField.ClassType).Create(self);
  // or if the owner must be the same too.
  Result := TFieldClass(ASrcField.ClassType).Create(ASrcField.Owner);
end;
0
 
LVL 17

Accepted Solution

by:
geobul earned 125 total points
ID: 9649491
Hi,

Try this:

function CreateNewFieldInstance(ASrcField: TField): TField;
begin
  Result := TFieldClass(GetClass(ASrcField.ClassName)).Create(nil);
end;

procedure TForm1.Button1Click(Sender: TObject);
var f: TField;
begin
  f := CreateNewFieldInstance(Table1.Fields[0]);
  ShowMessage(f.ClassName);
  f.Free;
end;

initialization
  RegisterClasses([TIntegerField, TStringField]);

finalization
  UnRegisterClasses([TIntegerField, TStringField]);

end.

Regards, Geo
0
 
LVL 17

Expert Comment

by:geobul
ID: 9649499
Too late ;-)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 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