7

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.

Bart
  • 19,692
  • 7
  • 68
  • 77
Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • 1
    Take out ": cardinal". Problem solved. You do NOT need to use TYPE DECLARATIONS and in fact you need to NOT use them. – Warren P Jun 08 '10 at 17:22
  • Yes, I know. But I explicitly wrote "assume for this question that you need to declare a typed constant as in"... – Andreas Rejbrand Jun 08 '10 at 17:30

3 Answers3

12

Depending on why you need the constant to be typed you can try something like

const
  MY_REAL_CONST = Cardinal($12345678);
  MY_CONST: Cardinal = MY_REAL_CONST;

case MyExpression of
  MY_REAL_CONST: { Do Something };
  ...
end;
Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83
  • 1
    @Andreas: If you find this to be a better answer than mine, you can accept it instead. SO allows you to change the accepted answer. – Mason Wheeler Jun 09 '10 at 12:45
  • @Mason Wheeler: You are right. Although I agree that most problems can be solevd without typed constants (and eventually I rewrote my code using normal constants), this is an answer to the actual question, so I will accept it. – Andreas Rejbrand Jun 09 '10 at 14:54
4

If you won't alter the value of the constant, then you don't need it to be a typed constant. The compiler can take the number you declare and correctly place it into whatever variable or parameter you assign it to. Typed constants are sort of a hack, and they're actually implemented as variables, so the compiler can't use them as constants whose value needs to be fixed at compile-time.

Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
  • I know. But this does not answer the question, for I did write "assume for this question that you need to declare a typed constant as in". But generally, I agree: I would also answer a question like this: "Why the **** do you need to use typed constants?". – Andreas Rejbrand Jun 08 '10 at 17:05
  • But still, the more I think about it, the more I realise that you should "hack" something else if this is an issue... – Andreas Rejbrand Jun 08 '10 at 17:12
  • 6
    *IF* you are using a typed constant because you want it to be a specific type (eg Cardinal in this example) you can declare as MyConst = Cardinal($FFFFFFFE); case MyExpression of MY_CONST: { Do Something }; ... end; – Remko Jun 08 '10 at 17:22
0

Typed constants can not be used in case statements, because a typed constant is actually more a static variable (and assignable...), and thus can't serve in a case statement, which expects constants.

Stijn Sanders
  • 35,982
  • 11
  • 45
  • 67