Q: Accessing fields of a polymorphic descendant from a typesafe list In Delphi 7, how do I design a descendant with polymorphism, so that the descendants can have a value field that is different from, or missing in the ancestor? I have a typesafe list that returns TMyObject like below.
But the Value field in the descendants below is not visible when I try to access it from the list. No surprise perhaps, since it doesn't exist in the base class, but how to handle this situation? I need to access the value of the descendant:
The typesafe list, containing TIntegerObjects and TSingleObjects:
List[Index].Value  <-- Doesn't exist!

List is typesafe and returns TMyObject items. 
The TMyObject, and its descendants:
TMyObject = class
  procedure SaveToStream; virtual; abstract;
  procedure LoadFromStream; virtual; abstract;
end;

TIntegerObject = class(TMyObject)
  Value: Integer;
  procedure SaveToStream; override;
  procedure LoadFromStream; override;
end;

TSingleObject = class(TMyObject)
  Value: Single;
  procedure SaveToStream; override;
  procedure LoadFromStream; override;
end;


A: Use the is operator to test if the object is of a certain class.
var
  obj: TMyObject;
  iValue: Integer;
  sValue: Single;
...
obj := List[index];
if obj is TIntegerObject then
  iValue := TIntegerObject(obj).Value // Access obj as TIntegerObject
else
if obj is TSingleObject then
  sValue := TSingleObject(obj).Value; // Access obj as TSingleObject

