Delphi中如何验证类型声明内变量的具体数值?
- 内容介绍
- 文章标签
- 相关推荐
本文共计201个文字,预计阅读时间需要1分钟。
如何确定变量的值是否在类型声明的范围之内。例如,类型TManagerType定义了三种类型:mtBMGR、mtAMGR、mtHOOT。var ManagerType: TManagerType; ... procedure DoSomething; begin if (ManagerType in TManagerType) then DoSomething else DoNothing; 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))
编译器不会根据单个子范围执行针对立即设置检查成员资格的优化.因此,[成熟]优化的代码将不那么优雅.
本文共计201个文字,预计阅读时间需要1分钟。
如何确定变量的值是否在类型声明的范围之内。例如,类型TManagerType定义了三种类型:mtBMGR、mtAMGR、mtHOOT。var ManagerType: TManagerType; ... procedure DoSomething; begin if (ManagerType in TManagerType) then DoSomething else DoNothing; 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))
编译器不会根据单个子范围执行针对立即设置检查成员资格的优化.因此,[成熟]优化的代码将不那么优雅.

