yes, VO was leaving when a match was found. But if you have a thousend CASE's und the last one matches, it did 999 comparisons/test first. The same with X# of course. In the case of sDeveloper:ToUpper() it will execute this method call 999! times for nothing.
SWITCH does one test and then knows what to do.
But switch does more: it checks at compile time if there are to identical conditions.
A very interesting and clear explanation you gave here.
The first time I used Switch in C# I didn't realize it continued after a matching condition was found. You have to add code to leave the statement for every condition. I found it another example of VO being superior in language design to C#.
You explanation made it clear it does have advantages. If Switch were precompiled AND leaving the loop it would be an improvement. Now I have to choose between adding extra code to leave the statements (I dislike extra code) but a check on invalid values (+ extra speed but I never have more than a handful of conditions so that won't count) - or the good old DO..CASE which allows me to make mistakes.
// The SWITCH statement is a replacement for the DO CASE statement
// The biggest difference is that the expression (in this case sDeveloper) is only evaluated once.
// which will have a performance benefit over the DO CASE statement
//
using System.Collections.Generic
Function Start() as void
FOREACH VAR sDeveloper in GetDevelopers()
SWITCH sDeveloper:ToUpper()
CASE "FABRICE"
? sDeveloper, "France"
CASE "CHRIS"
CASE "NIKOS"
? sDeveloper, "Greece"
CASE "ROBERT"
? sDeveloper, "The Netherlands"
OTHERWISE
? sDeveloper, "Earth"
END SWITCH
NEXT
Console.ReadKey()
RETURN
As you can see, you don't need to break out in X#.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
personally I think we have "switch" and "do case" - the C# people does not have our "do case", and the extended switch format seems to be very similar to the xBase do case format.
IMHO we have more urgent things than language extensions at this moment, the runtime and the RDDs for example.
The X# language itself is very complete, and I like it very much to work with it. One of my favorites is the short property syntax.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
As you can see, you don't need to break out in X#.
Hello Wolfgang, Frank,
That's great - I made a note for it. So the X# Switch statement works as the C# should have been.
Not sure Frank what you meant with the link. It says that you need 'break' in every statement. Which is not needed in X# and that's a lot more logical and a lot less code, especially in your 999 statements large switch statements.