Results 1 to 5 of 5

Thread: Switch Statement **SOLVED**

  1. #1

    Thread Starter
    Hyperactive Member Sgt-Peppa's Avatar
    Join Date
    Mar 2003
    Location
    Munich - Germany
    Posts
    476

    Resolved Switch Statement **SOLVED**

    Hey all,
    this is really emberassing

    I want to use a switch statement to check for some Integer values
    Code:
    switch (x)
    {
        case 0:
    		doSomething();
    		break;
        case 1:
        	doSomethingElse();
        default:
        	doNothing();
            break;
    }
    This is working. But is there a way that I can have more than one Integer inside my case Statement?
    Like
    Code:
    switch (x)
    {
        case 0,1,2:
    		doSomething();
    		break;
        case 2,3,4:
        	doSomethingElse();
        default:
        	doNothing();
            break;
    }
    Thanks for the help!

    Stephan
    Last edited by Sgt-Peppa; Aug 19th, 2005 at 07:22 AM. Reason: SOLVED
    Keep Smiling - even if its hard
    Frankie Says Relax, wossname Says Yeah!
    wossname:--Currently I'm wearing a gimp suit and a parachute.
    C# - Base64 Blog

  2. #2
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Switch Statement

    Something like this?

    Code:
            switch (myInt)
            {
                case 1:
                case 2:
                case 3:
                    Console.WriteLine("Your number is {0}.", myInt);
                    break;
                default:
                    Console.WriteLine("Your number {0} is not between 1 and 3.", myInt);
                    break;
            }



    - ØØ -

  3. #3

    Thread Starter
    Hyperactive Member Sgt-Peppa's Avatar
    Join Date
    Mar 2003
    Location
    Munich - Germany
    Posts
    476

    Re: Switch Statement

    Yes, that does the trick! I did not know you could have the code "fall" through! It works, thanks!

    Stephan
    Keep Smiling - even if its hard
    Frankie Says Relax, wossname Says Yeah!
    wossname:--Currently I'm wearing a gimp suit and a parachute.
    C# - Base64 Blog

  4. #4
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Switch Statement **SOLVED**

    Yes you can. Don't think you can have ranges like case10-20, but it should really be possible. Maybe it is though...but I am to tired to look it up...


    - ØØ -

  5. #5
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Switch Statement **SOLVED**

    sadly though, it looks like the control can only fall through if you don't have any line of code in the case block. I was looking for a fix of this before.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width