Question

Here I have a strange issue, I'm trying to replicate a behavior found in powerbuilder, to C#.

Here is the choose case statement in powerbuilder

CHOOSE CASE ls_char
   CASE "_" 
      ls_char = "-"
   CASE "0" to "9"
      MESSAGEBOX("DIGIT")
END CHOOSE

Now here comes the issue: ls_char can be a string or a char but still powerbuilder finds if it's equivalent numeric value falls between "0" and "9". We have even tried it by giving "0" TO "900" and giving ls_char = "300" and it pops the messagebox.

Any guidance regarding having some regex to achieve this or any other solution would be greatly appreciated.

Was it helpful?

Solution

In PowerScript CHOOSE statement doesn't compare "...equivalent numeric value...", it just compares objects as they are (in this case strings, without any further processing). "9" is greater than "0" as "999" is greater than "000" (you can try "11" is smaller than "2").

From PowerScript reference manual documentation for expressionlist parameter in CASE you can see TO clause accepts just values, conversion is usually made from testexpression type to expressionlist type(s) and not vice-versa. It means that if you provide strings then it'll compare strings instead of numbers!

There is not an equivalent of CHOOSE in C# and switch statement can't be used like that (you can find it, for example, in VB.NET for Select statement). Workaround is to use if instead:

string text = ls_char.ToString();
if (text == "_")
    ls_char = "-";
else if (text >= "0" && text <= "9")
    MessageBox.Show("Digit");

To compare numbers what you have to do is to provide numbers in CASE blocks:

CHOOSE CASE long(ls_char)
   CASE 0 TO 5
      MESSAGEBOX("SMALLER")
   CASE 6 TO 9
      MESSAGEBOX("SMALL")
END CHOOSE

In this case ls_char (whatever is its type) is converted to a number and then compared to check if it's in requested range. Roughly in C# it's equivalent to this:

int number = Convert.ToInt32(ls_char);
if (number >= 0 && number <= 5)
    MessageBox.Show("Smaller");
else if (number >= 6 && number <= 9)
    MessageBox.Show("Small");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top