10

I have a function which is being passed an integer value, representing a step value. There are 5 seperate conditions I want to test for: Value =0 Value =1 Value =-1 Value >1 Value <-1

Currently this is implemented as a set of if statements, and I would like to change this for a case statement. I have no problems with the specific value cases, or even a limited range (say 1..10) but how do i write a case representing Value >1 , or Value <-1?

HMcG
  • 2,062
  • 4
  • 24
  • 36

1 Answers1

27
var
  MyValue: integer;

...

case MyValue of
  Low(Integer)..-2:
    beep;
  -1:
    beep;
  0:
    beep;
  +1:
    beep;
  2..High(Integer):
    beep;
end;
Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • Ah, perfect! I knew there must be some way to do it, but it's not very obvious. Thanks. – HMcG Oct 02 '11 at 19:00
  • Great didn't new that case supports intervals greater than 255 i confused them with set of – opc0de Oct 02 '11 at 19:07
  • 2
    +1 might have to refactor this to `low(NativeInt)` and `high(NativeInt)` in XE2 ;-) – David Heffernan Oct 02 '11 at 19:25
  • 5
    @Downvoter Hardly. If a variable is typed as `Integer` then that would be fine. In fact `Integer` is always 32 bits (I'm ignoring D1) and `NativeInt` is the one that varies. It would be more robust to write `low(Tag)` which I presume would compile fine. – David Heffernan Oct 02 '11 at 20:10
  • @David Heffernan, thats not correct fact, read up on generic vs. fundamental types. – Premature Optimization Oct 02 '11 at 20:47
  • @David Heffernan, yes, when generic types can vary per platform, one should use fundamental types – Premature Optimization Oct 02 '11 at 21:57
  • @Downvoter Actually what varied here is the type of `Tag`. It was `Integer` in XE and earlier. In XE2 it has become `NativeInt`. In case `Tag` is `Integer` then it is correct to write `low(Integer)` since if `Integer` varies size (which it doesn't) then `low()` will vary too. Now that `Tag` is `NativeInt` which does vary per platform then you cannot use fundamental types since they do not. What you must do is use the *same* type as the variable used in the case statement. And in XE2 that is `NativeInt`. But best of all is to write `low(Tag)` which will always be correct. – David Heffernan Oct 02 '11 at 22:04
  • @David Heffernan, `Integer` size varies even within Borland implementations which always been nailed to x86 platform. – Premature Optimization Oct 02 '11 at 23:46
  • BTW, `Tag` name isnt much informative here, @Andreas Rejbrand. Original documentation uses `selector-expression` (which by definition is of `ordinal` type). – Premature Optimization Oct 02 '11 at 23:50
  • @Downvoter - yes, Integer is 16-bit in Delphi 1.0 and 32-bit in all other Delphi versions What other implementation variations are you thinking of, and just how likely is it do we think that this question and any answers to it, will be compiled using Delphi 1.0 ? Just curious. – Deltics Oct 03 '11 at 00:47
  • @Deltics, your sentence perfectly illustrates unportability of borland implementations. [Pascal has been subsetted, modified and worse.](http://www.standardpascal.org/) OP asks about Pascal construct, not Delphi specific one, so lets not use vendor lock-in here. – Premature Optimization Oct 03 '11 at 14:59
  • 2
    @Downvoter... The OP makes NO mention of "Pascal" anywhere but specifically mentions "Delphi" in the question and has tagged the question as "delphi". You might want to step OUT of the light for a minute... it appears to be getting in your eyes. ;) – Deltics Oct 03 '11 at 20:35