Based on an earlier post, I ve written the following code. Please excuse the verbosity of this post. I believe it s better for all parties to have the full code available to test and comment on.
program sandbox;
{$APPTYPE CONSOLE}
uses
SysUtils,
Generics.Collections;
type
TDataType = class
// Stuff common to TInt and TStr
end;
TInt = class(TDataType)
FValue: integer;
constructor Create(Value, Low, High: integer);
end;
TStr = class(TDataType)
FValue: string;
constructor Create(Value: string; Length: integer);
end;
TSomeClass = class
FIntList: TList<TInt>;
FStrList: TList<TStr>;
procedure AddToList<T: TDataType>(Element: T);
constructor Create();
procedure Free();
end;
constructor TInt.Create(Value, Low, High: Integer);
begin
inherited Create();
FValue := Value;
end;
constructor TStr.Create(Value: string; Length: Integer);
begin
inherited Create();
FValue := Value;
end;
procedure TSomeClass.AddToList<T>(Element: T);
begin
if TObject(Element) is TInt then
FIntList.Add(Element)
else if TObject(Element) is TStr then
FStrList.Add(Element);
end;
constructor TSomeClass.Create();
begin
inherited;
FIntList := TList<TInt>.Create();
FStrList := TList<TStr>.Create();
end;
procedure TSomeClass.Free();
var
SomeIntItem: TInt;
SomeStrItem: TStr;
begin
for SomeIntItem in FIntList do begin
SomeIntItem.Free();
end;
for SomeStrItem in FStrList do begin
SomeStrItem.Free;
end;
FIntList.Free();
FStrList.Free();
end;
var
Inst: TSomeClass;
begin
try
{ TODO -oUser -cConsole Main : Insert code here }
Inst := TSomeClass.Create;
Inst.AddToList(TInt.Create(100, 0, 101));
Inst.AddToList(TStr.Create( Test , 10));
Inst.Free;
except
on E:Exception do
Writeln(E.Classname, : , E.Message);
end;
end.
Note that the constructors of TInt
and TStr
in the real world would utilize the Low, High: integer
and Length: integer
parameters as well. I m having an "E2089 Invalid typecast" at if TObject(Element) is TInt then
and else if TObject(Element) is TStr then
running Delphi 2009. Do anyone know why this happens?
Edit: Please note that TInt
and TStr
are just two of possibly 10-20 other types; otherwise overloading is the tool for the job. :)