Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / May 2007

Tip: Looking for answers? Try searching our database.

case values in switch statement?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andrew - 02 May 2007 17:09 GMT
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.
r norman - 02 May 2007 17:29 GMT
>Hi, guys,
>
[quoted text clipped - 11 lines]
>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;
Peter Duniho - 02 May 2007 17:38 GMT
> [...]
> 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
Jani Järvinen [MVP] - 02 May 2007 19:01 GMT
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.

Signature

Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
janij@removethis.dystopia.fi
http://www.saunalahti.fi/janij/

Andrew - 02 May 2007 22:26 GMT
I used if. Thank you guys.

> Hello!
>
[quoted text clipped - 25 lines]
> 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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.