PC SOFT

FORUMS PROFESSIONNELS
WINDEVWEBDEV et WINDEV Mobile

Accueil → WINDEV 2024 → Switch - Case with Min and Max values
Switch - Case with Min and Max values
Débuté par Al, 12 juin 2005 18:59 - 5 réponses
Posté le 12 juin 2005 - 18:59
G'day All
The help doco for the new options in the Switch/case command is not making any sense at all to me.
I think I can test for a max and min value but can't figure out the syntax
I am trying to test a character to see if its ascii value falls inside a set range so what I am trying to do is :

Switch ChrToTest
Case >e and <= 90
do something
case >= 97 and <= 122
do something
End
This is the extent of the help from PCSoft
Case <Minimum expression> A <Maximum expression>: <Statement 9>
Case <Minimum expression> [<= * <=] [<=* <=] [<= * <] [< * <] <Maximum expression>: <Statement 11>

and an example which makes even less sense to me.
SWITCH True
CASE Value = 10: Trace(10)
CASE 10<Value<20 : Trace("In between")
CASE Value>30 : Trace("Greater than 30")
END
Any help with the syntax gratefully accepted
Regards
Al
Posté le 12 juin 2005 - 19:38
G'day All
The help doco for the new options in the Switch/case command is not making any sense at all to me.
I think I can test for a max and min value but can't figure out the syntax
I am trying to test a character to see if its ascii value falls inside a set range so what I am trying to do is :

Switch ChrToTest
Case >e and <= 90
do something
case >= 97 and <= 122
do something
End
This is the extent of the help from PCSoft
Case <Minimum expression> A <Maximum expression>: <Statement 9>
Case <Minimum expression> [<= * <=] [<=* <=] [<= * <] [< * <] <Maximum expression>: <Statement 11>

and an example which makes even less sense to me.
SWITCH True
CASE Value = 10: Trace(10)
CASE 10<Value<20 : Trace("In between")
CASE Value>30 : Trace("Greater than 30")
END

Any help with the syntax gratefully accepted
Regards
Al

Hi Al,
Try:
chrtotest is string = "b"
SWITCH True
CASE chrtotest >="a" AND chrtotest<= "z"
process
CASE chrtotest >= "A" AND chrtotest<= "Z"
process
END
The case statement that returns true will be executed.
Bert
Posté le 12 juin 2005 - 20:24
G'day Bert
Thanks for setting me on the right path, the example you gave works fine.
Regards
Al
Posté le 13 juin 2005 - 04:02
Hi Al
The following code will work for your example, I have it as a procedure running on the On Exit code of window edit control named ChrToTest
PROCEDURE CharConv()
ChrNum is int = Asc(ChrToTest) //Convert input to ASCII value

SWITCH True
CASE ChrNum <65: Trace("Less than 65")
CASE 65<=ChrNum<: Trace("Within 1st range")
CASE 91<=ChrNum<–: Trace("Out of range")
CASE 97<=ChrNum<2: Trace("Within 2nd range")
CASE ChrNum >122: Trace("Greater than 122")
END

If you look in the Help under Operator/Comparison it is explained as....
Comparison intervals
Comparison intervals enable you to simplify the statements for complex comparisons. For example, the following line

IF x>5 and x<10 THEN ...
can be replaced by
IF 5<x<10 THEN

which possibly makes a little more sense
HTH
Regards
DerekT
Posté le 13 juin 2005 - 05:15
G'day Derek
Thanks for the extra explanation, it is all starting to make sense now but the syntax is still foreign to my straight forward brain. The problem with these strange syntax constructions for me, is that I always have to think about them, they
never seem to become intuitive. (I could not write a complex condition clause for Hcreateview without the wizard.)
The concept of testing the value "true" against the result of a case is not normal for me when I am so used to testing the actual value in the Switch and the Comparison intervals seems quite odd at first glance when you are used to a standard verbose boolean construction and when reviewing code later on do not immediately make sense, if you only use the construction occasionally.
The ability to use ":" or ";" to issue a command a line continuation is also odd, but I never use them or "THEN" so it is no real issue. I prefer single lines of code with simple constructs that I can read years later and for that I am prepared to pay the price for additional time to execute the code,
While I may grumble about it (at 2:00 am in the morning), I consider that these things are just part of the very small price to pay for the continuing expansion of the language and the French origins. The overall benefits of Windev outweigh all these minor issues that possibly only annoy me.
Regards
Al
Posté le 13 juin 2005 - 19:08
...
>While I may grumble about it (at 2:00 am in the morning), I consider that these things are just part of the very small price to pay for the continuing expansion of the language and the French origins.
...
Hi Al,
I don't think it has anything to do with the French origins. I previously used an American language where I just had SWITCH on one line, followed by CASE statements, which in the end comes to the same as SWITCH true. I had to get used to put something after the Switch statement.
The reason I use mostly "SWITCH true" statemens is that I can test multiple values. The following example will execute the CASE statement (because vVal is 0) but this is of course wrong because vString is blank.
vVal is int
vString is string
SWITCH vVal
CASE 0 AND vString <> "" : Info("vVal is zero and vString is not blank")
END
Actually, I thought the above syntax wouldn't even be accepted but just discovered that it is. Like so many other things, I guess it's just a matter of habit.
Best regards
mat