Delphi Checking the value of a variable in a type declaration

How to determine if a variable's value is within the range of a type declaration. Example.

Type
  TManagerType = (mtBMGR, mtAMGR, mtHOOT);

...

var
  ManagerType: TManagerType;

....


procedure DoSomething;
begin
  if (ManagerType in TManagerType) then
    DoSomething
  else
    DisplayErrorMessage;
end;

Thanks, Peter.

+3
source share
3 answers
InRange: Boolean;
ManagerType: TManagerType;
...
InRange := ManagerType in [Low(TManagerType)..High(TManagerType)];

As noted by Nikolai O. - while the Boolean expression above directly corresponds to:

(Low(TManagerType) <= ManagerType) and (ManagerType <= High(TManagerType))
Compiler

does not perform optimization when checking membership against immediate dialing based on one subband. Thus, [mature] optimized code will be less elegant.

+5
source

, TManagerType , Pascal. , , - - .

:

InRange(ord(ManagerType), ord(low(ManagerType)), ord(high(ManagerType)))
+3

You should check this via: if mType> High (TManagerType), then ...

-1
source

Source: https://habr.com/ru/post/1777091/