Delphi 如何将属性与字符串索引一起使用?

Delphi 如何将属性与字符串索引一起使用?,delphi,delphi-7,Delphi,Delphi 7,我有以下代码: type TMyClass = class private procedure SetKeyValue(const Key: WideString; Value: Widestring); function GetKeyValue(const Key: WideString): WideString; public // this works property KeyValue[const Index: Wide

我有以下代码:

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
属性给我错误信息:

不兼容的类型:“字符串”和“整数”

我需要索引是字符串。
是否可以将
索引
与字符串值一起使用

这是不可能的。索引属性仅支持整数作为索引常量

参见(自己的重点):

索引说明符允许多个属性共享相同的访问方法,同时表示不同的值。索引说明符由指令
索引
后跟一个介于
-2147483647
2147483647
之间的整数常量组成。如果属性具有索引说明符,则其
read
write
说明符必须列出方法而不是字段


对于
索引
说明符,这是不可能的,因为它只支持整数索引。您必须使用一组单独的属性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;
它工作得很好

属性值[常量名称:字符串]:字符串读取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;


谢谢。我已经知道了。但是如果我需要为每个键编写setter/getter,那么这就忽略了属性索引的全部要点+1.这就是OP在其
KeyValue
属性中显示的内容。Delphi中的索引说明符是不同的。它允许您对许多属性使用相同的getter和setter,其中在getter中您可以通过索引说明符值进行切换。因此,这“有效”,并且是不同语言所知的索引说明符的可能实现,在Delphi中,它不是真正的索引说明符用法。答案是不能将字符串类型与索引说明符一起使用。您的意思是这样的吗?是的,这就是实际索引说明符的用法。许多属性使用相同的getter和setter。在它们的实现中,它们接收索引说明符值,并且可以决定如何处理要获取或设置的值。您所显示的内容很好,但它没有使用索引说明符。
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;