switch sytax ?

W

What-A-Tool

There are several differences in "switch", and VB's "Select Case"(which is
what I'm used to) that I can't quite figure. Could someone please point out
what is wrong with the following code - in particular the "case" with the
math operators :

char[] chs = words.ToCharArray(); //Create an array which contains all
entered charecters


foreach (char ch in chs) //Loop thru all charecters in array

{

intCurrChar ++; //Count charecters checked

switch(ch)

{

case '-': //negative sign

break;

case '.': //decimal point

break;

case (> = '0', <= '9'): //numeric charecter

break;


default: //any other charecter

return false;

}

}



Thanks - Sean
 
O

Octavio Hernandez

Hi,

You must use:

case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
// digit processing
break;
default:
// etc.

No ranges allowed in C#' cases.

Regards - Octavio
 
W

What-A-Tool

Octavio Hernandez said:
Hi,

You must use:

case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
// digit processing
break;
default:
// etc.

No ranges allowed in C#' cases.

Regards - Octavio

Now thats no fun!
Suppose I was testing a numeric variable - are math comparisons(<, >, ect.)
valid then?

Thanks - Sean
 
O

Otis Mukinfus

There are several differences in "switch", and VB's "Select Case"(which is
what I'm used to) that I can't quite figure. Could someone please point out
what is wrong with the following code - in particular the "case" with the
math operators :

char[] chs = words.ToCharArray(); //Create an array which contains all
entered charecters


foreach (char ch in chs) //Loop thru all charecters in array

{

intCurrChar ++; //Count charecters checked

switch(ch)

{

case '-': //negative sign

break;

case '.': //decimal point

break;

case (> = '0', <= '9'): //numeric charecter

break;


default: //any other charecter

return false;

}

}



Thanks - Sean
Here is MY simple explanation:

The C# switch will not evaluate expressions or "chained" variables.
The VB Select will evaluate expressions and "chained" variables.

If you need to evaluate expressions in C# you will need to use the if
construct.

The EXACT explanation is in the documentation of both.

Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
 
G

Guest

The J#/Java switch is even less fun! (has the C# restrictions and only works
for ordinal expressions)
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter
 
J

Jon Skeet [C# MVP]

What-A-Tool said:
Now thats no fun!
Suppose I was testing a numeric variable - are math comparisons(<, >, ect.)
valid then?

No. Use a series of if/else expressions if you don't want individual,
constant cases - the latter is what switch/case is for.
 
J

Jon Skeet [C# MVP]

David Anton said:
The J#/Java switch is even less fun! (has the C# restrictions and only works
for ordinal expressions)

Java doesn't allow you to switch on strings, but Java5 allows enums to
be switched on with cleaner syntax than C#'s, IMO.
 
D

Daniel O'Connell [C# MVP]

Jon Skeet said:
Java doesn't allow you to switch on strings, but Java5 allows enums to
be switched on with cleaner syntax than C#'s, IMO.

Out of curiosity, what specifically is cleaner?
 
J

Jon Skeet [C# MVP]

Daniel O'Connell said:
Out of curiosity, what specifically is cleaner?

You don't need to put the name of the enum in front of each case:

public enum MyEnum
{
Bar,
Baz
}

public class Test
{
public static void main (String[] args)
{
MyEnum x = MyEnum.Bar;

switch (x)
{
case Bar:
System.out.println ("Hello");
break;
case Baz:
System.out.println ("There");
break;
default:
System.out.println ("Neither");
}
}
}

As the type is going to be the same for every case, there doesn't seem
to be any point in specifying it everywhere...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top