|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
case values in switch statement?Hi, guys,
I have a switch statement like the follows in a KeyPress event: switch (e.KeyChar) { case '0' - '9': case 'A' - 'Z': break; default: break; } Of course, this did not work. But, how can I do such kind of case values which are in a range without writing each case value one by one? Thanks a lot. On Wed, 2 May 2007 09:09:01 -0700, Andrew
<And***@discussions.microsoft.com> wrote: Show quoteHide quote >Hi, guys, You can't.> >I have a switch statement like the follows in a KeyPress event: > > switch (e.KeyChar) > { > case '0' - '9': > case 'A' - 'Z': > break; > default: > break; > } > >Of course, this did not work. But, how can I do such kind of case values >which are in a range without writing each case value one by one? Thanks a lot. > With such large ranges it is easier to use if - else if statements. Otherwise you can do case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: // do digit stuff here break; case 'a': case 'b': case 'c': case 'd': ...... // do lower case alpha stuff here break; On Wed, 02 May 2007 09:09:01 -0700, Andrew
<And***@discussions.microsoft.com> wrote: > [...] You can't. Each "case" statement has to have a single value.> Of course, this did not work. But, how can I do such kind of case values > which are in a range without writing each case value one by one? Thanks > a lot. If you prefer a range for readability or ease of typing, you'll have to use if() statements. That may be more appropriate in your example, given the number of case statements that would be required. Pete Hello!
> You can't. Each "case" statement has to have a single value. Yes, this is true for C# which Andrew is using.I'd probably write several If statements although a Switch statement would be prettier. Or, I'd probably write a simple function to test the character and then use the return value in a switch statement (think "IsAlpha"). But since this is the .NET Framework newsgroup (and not a language specific one) I want to add that this is a "limitation" of the C# language, and not ..NET per se. For instance in Borland's (CodeGear's nowadays) Delphi for ..NET, it is perfectly possible to say: ------------------- var my_char : char; begin my_char := 'K'; case my_char of '0'..'9': MessageBox.Show('Number!'); 'A'..'Z': MessageBox.Show('Caps!'); end; end; ------------------- Or, you could use Delphi's set functionality and the "in" operator. Both of which are indeed more expressive than series of Ifs in C#. But you can't win always -- and this isn't definitely a reason to change languages. -- Regards, Mr. Jani Järvinen C# MVP Helsinki, Finland ja***@removethis.dystopia.fi http://www.saunalahti.fi/janij/ I used if. Thank you guys.
Show quoteHide quote "Jani Järvinen [MVP]" wrote: > Hello! > > > You can't. Each "case" statement has to have a single value. > > Yes, this is true for C# which Andrew is using. > > I'd probably write several If statements although a Switch statement would > be prettier. Or, I'd probably write a simple function to test the character > and then use the return value in a switch statement (think "IsAlpha"). > > But since this is the .NET Framework newsgroup (and not a language specific > one) I want to add that this is a "limitation" of the C# language, and not > ..NET per se. For instance in Borland's (CodeGear's nowadays) Delphi for > ..NET, it is perfectly possible to say: > > ------------------- > var my_char : char; > begin > my_char := 'K'; > case my_char of > '0'..'9': MessageBox.Show('Number!'); > 'A'..'Z': MessageBox.Show('Caps!'); > end; > end; > ------------------- > > Or, you could use Delphi's set functionality and the "in" operator. Both of > which are indeed more expressive than series of Ifs in C#. But you can't win > always -- and this isn't definitely a reason to change languages. > > -- > Regards, > > Mr. Jani Järvinen > C# MVP > Helsinki, Finland > ja***@removethis.dystopia.fi > http://www.saunalahti.fi/janij/ > > >
Other interesting topics
Locking Desktop and Threading Issue
The Interlocked on the Edge of Forever Determining if two paths are the same Questions about clickOnce Truly cross platform? Windows Workflow - What basic tutorials? What's going on here? Overriding "OnXXX" versus adding event handler convert an object to string and back (in a culture independent fashion) some config error happens periodically only .Net Windows Service ...data access problem |
|||||||||||||||||||||||