I came across an interesting situation that I shall try to describe.
type
TObject1 = class(TObject)
....
end;
TObject2 = class(TObject)
....
end;
implimentation
procedure ABC(aObject: TObject);
var
LocalObject: TObject;
begin
LocalObject := TypeOf(aObject).Create;
:
LocalObject.Free;
end;
var
MyObject2: TObject2;
begin
ABC(MyObject2);
end;
I would like to pass the ABC procedure a descendant of TObject, and inside the ABC procedure create an instance of the descendant object (not of TObject) and copy the object that was passed in. In the above example, 'TypeOf' of course does not compile. At run time, the type of the aObject is known so it should be possible to create an object of whatever type aObject is and assign it to the LocalObject variable. I just don't know how?
I would rather not do the following solution;
procedure ABC(aObject: TObject);
var
LocalObject: TObject;
begin
if aObject is TObject1 then
LocalObject := TObject1.Create;
if aObject is TObject2 then
LocalObject := TObject2.Create;
:
end;
Can anyone help???
Thanks
type
TObject1 = class(TObject)
....
end;
TObject2 = class(TObject)
....
end;
implimentation
procedure ABC(aObject: TObject);
var
LocalObject: TObject;
begin
LocalObject := TypeOf(aObject).Create;
:
LocalObject.Free;
end;
var
MyObject2: TObject2;
begin
ABC(MyObject2);
end;
I would like to pass the ABC procedure a descendant of TObject, and inside the ABC procedure create an instance of the descendant object (not of TObject) and copy the object that was passed in. In the above example, 'TypeOf' of course does not compile. At run time, the type of the aObject is known so it should be possible to create an object of whatever type aObject is and assign it to the LocalObject variable. I just don't know how?
I would rather not do the following solution;
procedure ABC(aObject: TObject);
var
LocalObject: TObject;
begin
if aObject is TObject1 then
LocalObject := TObject1.Create;
if aObject is TObject2 then
LocalObject := TObject2.Create;
:
end;
Can anyone help???
Thanks