Delphi中类型声明变量值检查,如何确保其正确无误?
- 内容介绍
- 文章标签
- 相关推荐
本文共计227个文字,预计阅读时间需要1分钟。
如何确定变量的值是否在类型声明的范围之内。例如,对于枚举类型`TManagerType`,包含`mtBMGR`, `mtAMGR`, `mtHOOT`,如何判断变量`ManagerType`的值是否属于这个范围。
pascalvar 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))
编译器不会根据单个子范围执行针对立即设置检查成员资格的优化.因此,[成熟]优化的代码将不那么优雅.
本文共计227个文字,预计阅读时间需要1分钟。
如何确定变量的值是否在类型声明的范围之内。例如,对于枚举类型`TManagerType`,包含`mtBMGR`, `mtAMGR`, `mtHOOT`,如何判断变量`ManagerType`的值是否属于这个范围。
pascalvar 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))
编译器不会根据单个子范围执行针对立即设置检查成员资格的优化.因此,[成熟]优化的代码将不那么优雅.

