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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

14 Experts available now in Live!

Get 1:1 Help Now