如何确定变量的值是否在“类型声明”的范围内. 防爆. Type TManagerType = (mtBMGR, mtAMGR, mtHOOT);...var ManagerType: TManagerType;....procedure DoSomething;begin if (ManagerType in TManagerType) then DoSomething else Di
防爆.
Type TManagerType = (mtBMGR, mtAMGR, mtHOOT); ... var ManagerType: TManagerType; .... procedure DoSomething; begin if (ManagerType in TManagerType) then DoSomething else DisplayErrorMessage; end;
谢谢,彼得.
InRange: Boolean; ManagerType: TManagerType; ... InRange := ManagerType in [Low(TManagerType)..High(TManagerType)];
正如Nickolay O.所说 – 虽然上面的布尔表达式直接对应于:
(Low(TManagerType) <= ManagerType) and (ManagerType <= High(TManagerType))
编译器不会根据单个子范围执行针对立即设置检查成员资格的优化.因此,[成熟]优化的代码将不那么优雅.