如何在类中正确地将Set设置为属性? [英] How do I correctly implement a Set in a class as a property?
问题描述
假设我有以下示例:
TDelphiIDECompatibility =(
Delphi1,
Delphi2 ,
Delphi3);
从类中,如何正确地将以上内容作为属性实现?
这个想法是,在我的组件中,我想要一个字段,该字段允许您为Set中的某些元素选择True或False。 / p>
我试图这样声明,但运气不佳:
TMyClass =类
私有
FIDE兼容性:TDelphiIDE兼容性的集合;
public
构造函数Create;
析构函数销毁;覆盖
属性IDECompatibility:TDelphiIDECompatibility读
FIDECompatibility写FIDECompatibility;
结尾;
错误消息为:
不兼容的类型:'TDelphiIDECompatibility'和'Set'
我知道的快速方法是将它们声明为常规布尔值,如下所示:
private
FDelphi1Compatible:Boolean;
FDelphi2Compatible:布尔值;
FDelphi3Compatible:布尔值;
public
构造函数Create;
析构函数销毁;覆盖
属性Delphi1Compatible:布尔读取
FDelphi1Compatible写FDelphi1Compatible;
结尾;
但是当我可以在Set /中定义它们时,我真的不喜欢这样。枚举?
我应该怎么做才能正确地声明它?
谢谢。
从TLama的评论中,我查看了Delphi的Anchors源,并在此处提出了解决方案:
TDelphiIDECompatibilityKind =(
Delphi1,
Delphi2,
Delphi3);
TDelphiIDECompatibility = TDelphiIDECompatibilityKind的集合;
和类:
private
FIDE兼容性:TDelphiIDE兼容性;
public
构造函数Create;
析构函数销毁;覆盖
属性IDECompatibility:TDelphiIDECompatibility读
FIDECompatibility写FIDECompatibility;
结尾;
Suppose I have the following as an example:
TDelphiIDECompatibility = (
Delphi1,
Delphi2,
Delphi3);
From a class, how could I implement the above correctly as a property?
The idea is that in my component I want to have a field that will allow you to select True or False for certain elements in a Set.
I tried to declare like this without much luck:
TMyClass = class
private
FIDECompatibility: Set of TDelphiIDECompatibility;
public
constructor Create;
destructor Destroy; override;
property IDECompatibility: TDelphiIDECompatibility read
FIDECompatibility write FIDECompatibility;
end;
The error message been:
Incompatible types: 'TDelphiIDECompatibility' and 'Set'
The quick way I know is to just declare them as regular booleans, like so:
private
FDelphi1Compatible: Boolean;
FDelphi2Compatible: Boolean;
FDelphi3Compatible: Boolean;
public
constructor Create;
destructor Destroy; override;
property Delphi1Compatible: Boolean read
FDelphi1Compatible write FDelphi1Compatible;
end;
But I don't really like having it like that when I can have them defined in a Set/Enumeration?
What should I be doing to declare it properly instead?
Thank you.
From TLama's comment I looked at the Delphi source for Anchors and came up with the solution here:
TDelphiIDECompatibilityKind = (
Delphi1,
Delphi2,
Delphi3);
TDelphiIDECompatibility = set of TDelphiIDECompatibilityKind;
And the class:
private
FIDECompatibility: TDelphiIDECompatibility;
public
constructor Create;
destructor Destroy; override;
property IDECompatibility: TDelphiIDECompatibility read
FIDECompatibility write FIDECompatibility;
end;
这篇关于如何在类中正确地将Set设置为属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!