加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Delphi检查类型声明中的变量值

发布时间:2020-12-15 09:06:18 所属栏目:大数据 来源:网络整理
导读:如何确定变量的值是否在“类型声明”的范围内. 防爆. Type TManagerType = (mtBMGR,mtAMGR,mtHOOT);...var ManagerType: TManagerType;....procedure DoSomething;begin if (ManagerType in TManagerType) then DoSomething else DisplayErrorMessage;end;
如何确定变量的值是否在“类型声明”的范围内.
防爆.

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))

编译器不会根据单个子范围执行针对立即设置检查成员资格的优化.因此,[成熟]优化的代码将不那么优雅.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读