Delphi:用于索引 TStringList 项的 Integer 以外的类型
可以使用用户定义的枚举类型对数组进行索引。例如:
type
TIndexValue = (ZERO = 0, ONE, TWO, THREE, FOUR);
var
MyArray: array[Low(TIndexValue) .. High(TIndexValue)] of String;
然后可以使用 TIndexValue
值作为索引来引用此数组中的元素:
MyArray[ZERO] := 'abc';
我正在尝试使用 TStringList
获得相同的通用功能>。
一个简单的解决方案是在引用时将每个索引值转换为 Integer
类型:
MyStringList[Integer(ZERO)] := 'abc';
另一种解决方案(隐藏所有转换)是创建 TStringList
的子类并将所有转换推迟到该子类的访问继承的 Strings 属性的子例程:
type
TIndexValue = (ZERO = 0, ONE, TWO, THREE, FOUR);
type
TEIStringList = class(TStringList)
private
function GetString(ItemIndex: TIndexValue): String;
procedure SetString(ItemIndex: TIndexValue; ItemValue: String);
public
property Strings[ItemIndex: TIndexValue]: String
read GetString write SetString; default;
end;
function TEIStringList.GetString(ItemIndex: TIndexValue): String;
begin
Result := inherited Strings[Integer(ItemIndex)];
end;
procedure TEIStringList.SetString(ItemIndex: TIndexValue; ItemValue: String);
begin
inherited Strings[Integer(ItemIndex)] := ItemValue;
end;
这对于使用枚举类型 TIndexValue 的单个实现来说效果很好。
但是,我想为多个由不同枚举类型索引的不同 TStringList
对象重复使用相同的逻辑或子类,而不必为每个可能的定义 TStringList
子类枚举类型。
这样的事情可能吗?我怀疑我可能必须依赖 Delphi 的泛型,但我非常有兴趣了解有更简单的方法来实现这一点。
Arrays
can be indexed using user-defined enumerated types. For example:
type
TIndexValue = (ZERO = 0, ONE, TWO, THREE, FOUR);
var
MyArray: array[Low(TIndexValue) .. High(TIndexValue)] of String;
Elements from this array can then be referenced using TIndexValue
values as an index:
MyArray[ZERO] := 'abc';
I am trying to obtain this same general functionality with a TStringList
.
One simple solution is to cast every index value to an Integer
type at the time of reference:
MyStringList[Integer(ZERO)] := 'abc';
Another solution (to hide all the casting) is to create a subclass of TStringList
and defer all the casting to this subclass's subroutines that access the inherited Strings
property:
type
TIndexValue = (ZERO = 0, ONE, TWO, THREE, FOUR);
type
TEIStringList = class(TStringList)
private
function GetString(ItemIndex: TIndexValue): String;
procedure SetString(ItemIndex: TIndexValue; ItemValue: String);
public
property Strings[ItemIndex: TIndexValue]: String
read GetString write SetString; default;
end;
function TEIStringList.GetString(ItemIndex: TIndexValue): String;
begin
Result := inherited Strings[Integer(ItemIndex)];
end;
procedure TEIStringList.SetString(ItemIndex: TIndexValue; ItemValue: String);
begin
inherited Strings[Integer(ItemIndex)] := ItemValue;
end;
This works fine for a single implementation that uses the enumerated type TIndexValue
.
However, I would like to re-use this same logic or subclass for several different TStringList
objects that are indexed by different enumerated types, without having to define TStringList
subclasses for each possible enumerated type.
Is something like this possible? I suspect I may have to depend on Delphi's Generics, but I would be very interested to learn that there are simpler ways to achieve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为泛型将是迄今为止最优雅的解决方案。使用它们就像重写上面的类一样简单:
然后用 T 替换所有 TIndexValue 引用。然后您可以像任何其他泛型一样创建它:
如果您坚持避免泛型,也许运算符重载会有用。像下面这样的东西应该可以工作:
然后使用:
更新:
您可以通过添加 Next、HasNext 等方法来调整 TIndexValueHolder 以在 for 或 while 循环中使用。不过,这可能最终会达不到目的。我仍然不确定目的是什么,或者为什么这会有用,但无论如何,这里有一些关于如何做到这一点的想法。
I think that generics would be by far the most elegant solution. Using them would be as simple as rewriting your class above as:
and then replacing all TIndexValue references with T. Then you could create it just as any other generic:
If you insist on avoiding generics, maybe operator overloading would be of use. Something like the following should work:
Then use with:
UPDATE:
You could adapt TIndexValueHolder for use in a for or while loop by adding Next, HasNext, etc. methods. This might end defeating the purpose, though. I'm still not sure what the purpose is, or why this would be useful, but here's some ideas for how to do it, anyways.
您可能可以使用类帮助器并将默认属性索引声明为变体:
测试代码:
查看以前不太成功的尝试的编辑历史记录。
You probably can use a class helper and declare the default property index as Variant:
Testing code:
See edit history for a previous less successful attempt.