Hi at all

Hi at all,
this is an example, I'd like to implement a list of my generic class. Is it possible?
I've googled a lot whithout success... :(
Can someone help me?
Thanks.

TPlcType = class
private
FValue: T;
procedure SetValue(const Value: T); overload;
procedure SetValue(const AValue: Boolean); overload;
procedure SetValue(const AValue: Byte); overload;
procedure SetValue(const AValue: Word); overload;
public
property Value: T read FValue write SetValue;
end;

TPlc = class
private
FMyBool: TPlcType;
FMyByte: TPlcType;
FMyWord: TPlcType;

FMyList: TOBjectList<...> // ????
end;

Comments

  1. Not possible without a common base class for TPlcType.

    ReplyDelete
  2. David Millington you are right.... I didn't explain well.
    Stefan Glienke you are right too because you understood what I thought.

    I want to replace my generics TPlcType <> with an object list to iterate all elements, but how Stefan Glienke said, I can not do that without a common base class.

    I drowned in an inch of water.
    Thanks.

    ReplyDelete
  3. Quick example how I would model it:

    type
    TPlcType = class
    protected
    function GetValueNonGeneric: TValue; virtual; abstract;
    procedure SetValueNonGeneric(const Value: TValue); virtual; abstract;
    public
    property Value: TValue read GetValueNonGeneric write SetValueNonGeneric;
    end;

    TPlcType = class(TPlcType)
    private
    FValue: T;
    procedure SetValue(const Value: T);
    protected
    function GetValueNonGeneric: TValue; override;
    procedure SetValueNonGeneric(const Value: TValue); override;
    public
    property Value: T read FValue write SetValue;
    end;

    TPlc = class
    private
    FMyBool: TPlcType;
    FMyByte: TPlcType;
    FMyWord: TPlcType;
    FMyList: TObjectList;
    end;

    { TPlcType }

    function TPlcType.GetValueNonGeneric: TValue;
    begin
    Result := TValue.From(FValue);
    end;

    procedure TPlcType.SetValue(const Value: T);
    begin
    FValue := Value
    end;

    procedure TPlcType.SetValueNonGeneric(const Value: TValue);
    begin
    FValue := Value.AsType;
    end;

    ReplyDelete

Post a Comment