Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
193 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 250 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

783 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