case values in switch statement?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
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;
 
[...]
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
 
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
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
I used if. Thank you guys.


Jani Järvinen said:
Hello!


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
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
Back
Top