Home All Groups Group Topic Archive Search About

case values in switch statement?

Author
2 May 2007 4:09 PM
Andrew
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.
Author
2 May 2007 4:29 PM
r norman
On Wed, 2 May 2007 09:09:01 -0700, Andrew
<And***@discussions.microsoft.com> wrote:

Show quoteHide quote
>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.
>

You can't.

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;
Are all your drivers up to date? click for free checkup

Author
2 May 2007 4:38 PM
Peter Duniho
On Wed, 02 May 2007 09:09:01 -0700, Andrew 
<And***@discussions.microsoft.com> wrote:

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

You can't.  Each "case" statement has to have a single value.

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
Author
2 May 2007 6:01 PM
Jani Järvinen [MVP]
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/
Author
2 May 2007 9:26 PM
Andrew
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/
>
>
>

Bookmark and Share