delphi 如何使用属性与字符串索引?

wqnecbli  于 2023-03-01  发布在  其他
关注(0)|答案(4)|浏览(217)

我有这个代码:

type
  TMyClass = class
    private
      procedure SetKeyValue(const Key: WideString; Value: Widestring);
      function GetKeyValue(const Key: WideString): WideString;
    public
      // this works
      property KeyValue[const Index: WideString] : WideString read GetKeyValue write SetKeyValue;

      // this does not compile
      // [Error]: Incompatible types: 'String' and 'Integer'
      property Speed: WideString index 'SPEED' read GetKeyValue write SetKeyValue;
  end;

Speed属性给我错误:
不兼容的类型:"字符串"和"整数"
我需要索引为字符串。是否可以使用字符串值的index

yeotifhr

yeotifhr1#

这是不可能的。索引属性只支持Integer作为索引常量。
参见文档(强调):
索引说明符允许多个属性共享相同的访问方法,但表示不同的值。索引说明符由指令index-21474836472147483647之间的整数常量组成。如果属性具有索引说明符,则其readwrite说明符必须列出方法而不是字段。

vtwuwzda

vtwuwzda2#

这在index说明符中是不可能的,因为它只支持整数索引。你必须使用一组单独的属性getter/setter方法:

type
  TMyClass = class
  private
    ...
    procedure SetSpeed(const Value: WideString);
    function GetSpeed: WideString;
  public
    ...
    property Speed: WideString read GetSpeed write SetSpeed;
  end;

procedure TMyClass.SetSpeed(const Value: WideString);
begin
  KeyValue['SPEED'] := Value;
end;

function TMyClass.GetSpeed: WideString;
begin
  Result := KeyValue['SPEED'];
end;
8e2ybdfx

8e2ybdfx3#

效果很好

    • 属性值[常量名称:字符串]:字符串读取GetValue写入SetValue;**
{ClassName=class}
private
 fArrayKey: Array of String;{1}
 fArrayValue: Array of String;{2}
 procedure  Add(const Name, Value: string);
 function GetValue(const Name: string): string;
 procedure SetValue(const Name, Value: string);
published
 property Values[const Name: string{1}]: string{2} read GetValue write SetValue;
function   {ClassName.}GetValue(const Name: string): string;
Var I:integer;
Begin
Result := '#empty';
  for I := low(fArrayKey) to high(fArrayKey) do
    if fArrayKey[i]=Name then
       Begin
         Result := fArrayValue[i];
         Break
       End;
If result='#empty' the Raise Exception.CreateFmt('Key %s not found',[name]);
End;
procedure  {ClassName.}SetValue(const Name, Value: string);
var i,j:integer
Begin
j:=-1;
  for I := low(fArrayKey) to high(fArrayKey) do
    if fArrayKey[i]=Name then
       Begin
         j:= i;
         Break
       End;
If j=-1 then Add(name,value) else fArrayValue[i]:= Value;
End;
procedure  {ClassName.}Add(const Name, Value: string);
begin
  SetLength(fArrayKey,Length(fArrayKey)+1);
  SetLength(fArrayValue,Length(fArrayValue)+1);
  fArrayKey  [Length(fArrayKey)  -1] := Name;
  fArrayValue[Length(fArrayValue)-1] := Value;
end;

http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Properties_(Delphi)

sh7euo9m

sh7euo9m4#

你可以这样做

type
  // Class with Indexed properties
  TRectangle = class
  private
    fCoords: array[0..3] of Longint;
    function  GetCoord(Index: string): Longint;
    procedure SetCoord(Index: string; Value: Longint);
  public
    property Left   : Longint Index 0 read GetCoord write SetCoord;
    property Top    : Longint Index 1 read GetCoord write SetCoord;

    property Right  : Longint Index 2 read GetCoord write SetCoord;
    property Bottom : Longint Index 3 read GetCoord write SetCoord;
    property Coords[Index: string] : Longint read GetCoord write SetCoord;
  end;
  
  const
  IndexCoordMap:array [0..3] of TIdentMapEntry
    (
    (Value: 0;   Name: 'Left'),
    (Value: 1;   Name: 'Top'),
    (Value: 2; Name: 'Right'),
    (Value: 3; Name: 'Bottom')
  );
  
  
  
  implementation

{$R *.dfm}

// TRectangle property 'Getter' routine
function TRectangle.GetCoord(Index: string): Longint;
var IntIndex: Integer;
begin
  // Only allow valid index values
  if (IdentToInt(Index, IntIndex, IndexCoordMap))
  then Result := fCoords[IntIndex]
  else raise Exception.Create('Invalid index');
end;

// TRectangle property 'Setter' routine
procedure TRectangle.SetCoord(Index: string; Value: Integer);
var IntIndex: Integer;
begin
  // Only allow valid index values
  if (IdentToInt(Index, IntIndex, IndexCoordMap))
  then fCoords[IntIndex] := Value
    else raise Exception.Create('Invalid index');
end;

相关问题