Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
C# switch Gripe
Message
From
02/01/2009 17:44:14
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Title:
Miscellaneous
Thread ID:
01370558
Message ID:
01370779
Views:
27
>>>Why in the world must a switch statement need an integer???
>>>
>>>Other languages are much more flexible in their CASE structures, so why would MS restrict the evaluation of the switch to an int????
>>
>>See Naomi's answer, but also be aware:
>>
>>If your trying to compare a switch statement to a VFP "DO CASE", they are very different. To write a VFP style case statement in C# use "else if" .
>
>Very different how? To me they seem quite similar. They are both structured as a series of checks of some value. When a match is found, take some action and exit. Both provide for a default action if no match is found.

The biggest difference is that a VFP Case statement takes a logical expression. A switch statement only evaluates if a specified value is equal to a certain case.
* This Case Statement can be written as a switch:
DO CASE
  CASE X = 1
    * Some Code
  CASE X = 2
    * Some Code
  OTHERWISE
    * Some Code
ENDCASE

* This Case Statement cannot be easily written as a switch:
DO CASE
  CASE X < 1
    * Some Code
  CASE X = 2 AND Y=3
    * Some Code
  OTHERWISE
    * Some Code
ENDCASE
If we map the second do case example over to c# we see that it is actually an else if structure:
if(X < 1)  // First Case
  {
  * Some Code
  }
else if (X == 2 && Y==3) // Second Case
  {
  * Some Code
  }
else // Otherwise
  {
  * Some Code
  }
If else is the same logical structure as VFP's DO CASE, it's just worded differently. I can't think of a scenario where I can't map a VFP DO CASE
directly into a C# if else.

Most of my VFP CASE statements however cannot be mapped to switch statements because the logical statements are not simple x=y type expressions.

However some can and when they can, switch adds some additional interesting features, like fall through and jumping that can be quite powerful. However as far as programming languages are concerned, DO CASE and switch are different control structures.
Previous
Reply
Map
View

Click here to load this message in the networking platform