我有以下内容:
TDirection = (dirNorth, dirEast, dirSouth, dirWest);
TDirections = set of TDirection;
Run Code Online (Sandbox Code Playgroud)
在一个单独的类中,我将它声明为属性:
property Directions: TDirections read FDirections write FDirections;
Run Code Online (Sandbox Code Playgroud)
我想要的是能够像对待布尔人一样对待它们,所以例如如果dirNorth是真的那么它将是1,如果是假的话0.
我想把它想象成 (1,0,0,0)
我想检查方向是否为True,我可以使用:
var
IsTrue: Boolean;
begin
IsTrue := (DirNorth in Directions);
Run Code Online (Sandbox Code Playgroud)
不确定上述是否正确,但我的另一个问题是如何更改其中一个方向True或False?
我现在达到了我的一个混乱状态:(
这是我尝试设置值的最后一件事,但我得到的是Illegal Expression(在Lazarus中).
Directions(TDirection(DirNorth)) := True;
Run Code Online (Sandbox Code Playgroud) 假设我有以下作为示例:
TDelphiIDECompatibility = (
Delphi1,
Delphi2,
Delphi3);
Run Code Online (Sandbox Code Playgroud)
从一个类,我怎么能正确地实现上述属性?
我的想法是,在我的组件中,我希望有一个字段,允许您为Set中的某些元素选择True或False.
我试图像这样宣布没有太多运气:
TMyClass = class
private
FIDECompatibility: Set of TDelphiIDECompatibility;
public
constructor Create;
destructor Destroy; override;
property IDECompatibility: TDelphiIDECompatibility read
FIDECompatibility write FIDECompatibility;
end;
Run Code Online (Sandbox Code Playgroud)
错误消息是:
不兼容的类型:'TDelphiIDECompatibility'和'Set'
我知道的快速方法是将它们声明为常规布尔值,如下所示:
private
FDelphi1Compatible: Boolean;
FDelphi2Compatible: Boolean;
FDelphi3Compatible: Boolean;
public
constructor Create;
destructor Destroy; override;
property Delphi1Compatible: Boolean read
FDelphi1Compatible write FDelphi1Compatible;
end;
Run Code Online (Sandbox Code Playgroud)
但是当我可以在Set/Enumeration中定义它们时,我真的不喜欢这样吗?
我该怎么做才能正确地宣布它呢?
谢谢.