Delphi Typed Constants in Case Statements

Posted by Andreas Rejbrand on Stack Overflow See other posts from Stack Overflow or by Andreas Rejbrand
Published on 2010-06-08T16:55:35Z Indexed on 2010/06/08 17:32 UTC
Read the original article Hit count: 300

Filed under:
|
|
|

What is the most elegant (or least ugly) way of using typed constants in a case statement in Delphi?

That is, assume for this question that you need to declare a typed constant as in

const
  MY_CONST: cardinal = $12345678;
  ...

Then the Delphi compiler will not accept

case MyExpression of
  MY_CONST: { Do Something };
  ...
end;

but you need to write

case MyExpression of
  $12345678: { Do Something };
  ...
end;

which is error-prone, hard to update, and not elegant.

Is there any trick you can employ to make the compiler insert the value of the constant (preferably by checking the value of the constant under const in the source code, but maybe by looking-up the value at runtime)? We assume here that you will not alter the value of the "constant" at runtime.

© Stack Overflow or respective owner

Related posts about delphi

Related posts about case