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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now