Hi

Hi

Can someone help me with class getters and setters. Class has a dynamic array in it. This is the code:

TRec = record
Name: string;
Address: string;
end;

TMyClass = class
private
FAlias: string;
FRec array of TRec;

function GetRec(const rec_name: string): TRec;
procedure SetRec(rec: TRec);
public
property Alias: string read FAlias;
property Rec: TRec read GetRec write SetRec;
end;

On line 'property Rec:' compiler gives me error E2008 Incompatible types.
If I take out 'read GetRec' it compiles fine. Is array in the class the problem?

Tnx.

Comments

  1. Zoran M Your original code in fact assumes that the property is a single record and not an array. Probably this is not that you meant. As Paul TOTH wrote you need an indexed property using string as index:

    private
    function GetRec(Index: String): TMyRec;
    procedure SetRec(Index: String; const Value: TMyRec);
    public
    property Rec[Index: String]: TMyRec read GetRec write SetRec;
    end;

    Now all you have to do is to implement access methods using Index to locate the needed item in the array. Or, better just use TDictionary.

    ReplyDelete
  2. Hard to say whether a pointer should be returned, I'd question that advice.

    ReplyDelete

Post a Comment