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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Installshield for Embarcadero EX 10.1 Berlin 4 72
enhance the following code 3 42
Firemonkey allowing RTL on android 6 56
scroll down TListBox component in Delphi 1 31
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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

763 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