我目前有一个选项管理,它使用属性,使用所有相同的getter / setter方法但使用索引。
对于Ints和Bools来说这很好用,但是我也希望这个能够用于枚举。
TIntegerOptions = (kInt1, kInt2, kInt3);
class property Integer1: Integer index kInt1 read GetOptionsValueInt write SetOptionsValueInt;
class property Integer2: Integer index kInt2 read GetOptionsValueInt write SetOptionsValueInt;
class property Integer3: Integer index kInt3 read GetOptionsValueInt write SetOptionsWertInt;
FOptionsListInt: array[TIntegerOptions] of IniIntObject; // Objects contain Inifile read&save functionalities
...
class function GetOptionsValueInt(const aOption: TIntegerOptions) : Integer
begin
Result := Ord(FOptionsListInt[aOption].GetValue());
end;
class procedure SetOptionsValueInt(const aOption: TIntegerOptions; const aValue: Integer);
begin
FOptionslistInt[aOption].ChangeTo(aValue);
end;
到目前为止这是有效的,现在我的问题是:
TEnumOptions = (kEnum1, kEnum2, kEnum3);
TEnum1 = (e1o1, e1o2, e1o3);
TEnum2 = (e2o1, e2o2, e2o3);
TEnum3 = (e3o1, e3o2, e3o3);
// these props fail because my functions return / excpect Integers, not the matching Enums
class property Enum1: TEnum1 index kEnum1 read GetOptionsValueInt write SetOptionsValueEnum;
class property Enum2: TEnum2 index kEnum2 read GetOptionsValueInt write SetOptionsValueEnum;
class property Enum3: TEnum3 index kEnum3 read GetOptionsValueInt write SetOptionsValueEnum;
FOptionsListEnum: array[TEnumOptions] of IniIntObject; // Objects contain Inifile read&save functionalities
我在代码中标记了问题。我可以以某种方式将getters / setter返回的Integer值转换为每个属性的匹配枚举吗?
对于那些阅读旧问题的人:
我决定只为枚举使用相同的getter / setter,因为最终它们也会被保存为Integers。如果我需要以某种方式在吸气剂内部投射,我会再次添加它们,但我希望在财产声明中找到解决方案。
答案 0 :(得分:2)
关于您的更新,您希望使用以下代码:
class property Enum1: TEnum1 index kEnum1 read GetOptionsValueInt;
无法编译,因为GetOptionsValueInt
返回类型为Integer
的值。由于属性的类型为TEnum1
,因此这是一种简单的类型不匹配。为了拥有类型TEnum1
的属性,getter必须是一个返回类型TEnum1
的值的函数。
如果每个属性都有不同的类型,则无法共享getter和setter。共享索引的getter和setter只能用于共享公共类型的属性。由于看起来这些属性都不共享类型,因此您将无法共享getter和setter。
原始问题的原始答案
您不会显示所有声明,因此我们很难知道您的代码无法编译的原因。我们不知道FOptionsListeEnum
是什么,我们也不知道Wert()
是什么。
但是,这是一个完整的程序,它演示了枚举类型可以用作索引属性的索引:
{$APPTYPE CONSOLE}
type
TMyEnum = (evOne, evTwo);
TMyClass = class
private
class function GetValue(const Option: TMyEnum): Integer; static;
public
class property ValueOne: Integer index evOne read GetValue;
class property ValueTwo: Integer index evTwo read GetValue;
end;
class function TMyClass.GetValue(const Option: TMyEnum): Integer;
begin
Result := ord(Option);
end;
begin
Writeln(TMyClass.ValueOne);
Writeln(TMyClass.ValueTwo);
end.
因此,这清楚地表明使用枚举类型作为索引没有问题。在哪种情况下,你的问题是什么。让我们来看看你的代码:
class function TKmpOption.GetOptionsWertEnum(const aOption: TEnumOptionen): Integer;
begin
Result := Ord(FOptionsListeEnum[aOption].Wert());
end;
class procedure TKmpOption.SetOptionsWertEnum(const aOption: TEnumOptionen; const aWert: Integer);
begin
FOptionslisteEnum[aOption].Aendern(aOption(aWert));
end;
如果Ord(FOptionsListeEnum[aOption].Wert())
无法使用类型不匹配错误进行编译,那么Wert()
似乎不是序数值。
至于完全没有意义的aOption(aWert)
。那永远不会编译。也许你的意思是TEnumOptionen(aWert)
,但这也毫无意义。为什么该值与用于索引可能选项的类型相同。
我希望上面的代码表明该问题与枚举类型无关,因为它们实际上是一种相当平淡无奇的性质。