如何将 属性 与字符串索引一起使用?
How to use property with string index?
我有这个代码:
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
属性 给我错误:
Incompatible types: 'String' and 'Integer'
我需要索引为字符串。
是否可以将 index
与字符串值一起使用?
那是不可能的。索引属性仅支持整数作为索引常量。
见documentation(自己强调):
Index specifiers allow several properties to share the same access method while representing different values. An index specifier consists of the directive index
followed by an integer constant between -2147483647
and 2147483647
. If a property has an index specifier, its read
and write
specifiers must list methods rather than fields.
这对于 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;
效果很好
属性 Values[const Name: string]: string read GetValue write 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)
我有这个代码:
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
属性 给我错误:
Incompatible types: 'String' and 'Integer'
我需要索引为字符串。
是否可以将 index
与字符串值一起使用?
那是不可能的。索引属性仅支持整数作为索引常量。
见documentation(自己强调):
Index specifiers allow several properties to share the same access method while representing different values. An index specifier consists of the directive
index
followed by an integer constant between-2147483647
and2147483647
. If a property has an index specifier, itsread
andwrite
specifiers must list methods rather than fields.
这对于 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;
效果很好
属性 Values[const Name: string]: string read GetValue write 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)