471,605 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,605 software developers and data experts.

switch sytax ?

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
Feb 19 '06 #1
9 3309
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
"What-A-Tool" <Di*****************@DieDieDie.Com> escribió en el mensaje
news:%S_Jf.5833$Tf3.5719@dukeread09...
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

Feb 19 '06 #2

"Octavio Hernandez" <oc*****************@gmail.com> wrote in message
news:e3**************@TK2MSFTNGP10.phx.gbl...
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
Feb 19 '06 #3
On Sun, 19 Feb 2006 09:01:37 -0500, "What-A-Tool"
<Di*****************@DieDieDie.Com> wrote:
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
Feb 19 '06 #4
Thanks to both of you - I'm clear now
Feb 19 '06 #5
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

"What-A-Tool" wrote:
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

Feb 19 '06 #6
What-A-Tool <Di*****************@DieDieDie.Com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 19 '06 #7
David Anton <Da********@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 19 '06 #8

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
David Anton <Da********@discussions.microsoft.com> wrote:
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.


Out of curiosity, what specifically is cleaner?
Feb 20 '06 #9
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:
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?


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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 20 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Myster Ious | last post: by
5 posts views Thread by Bryan Parkoff | last post: by
10 posts views Thread by clueless_google | last post: by
65 posts views Thread by He Shiming | last post: by
3 posts views Thread by pgraeve | last post: by
11 posts views Thread by ME | last post: by
12 posts views Thread by | last post: by
11 posts views Thread by =?Utf-8?B?anAybXNmdA==?= | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by CCCYYYY | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.