Case statement puzzle

This is a discussion on Case statement puzzle within the C# Programming forums, part of the General Programming Boards category; Can someone tell me what's wrong with this block of skeleton code so far?? Code: protected override void WndProc(ref Message ...

  1. #1
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271

    Case statement puzzle

    Can someone tell me what's wrong with this block of skeleton code so far??
    Code:
            protected override void WndProc(ref Message m) 
            {
                // Listen for operating system messages.
                switch (m.Msg)
                {
                    case DBT_DEVICEARRIVAL:                 
                            MessageBox.Show("A device has been docked");
                            break;                    
    
                    case DBT_DEVICEREMOVECOMPLETE:         
                            MessageBox.Show("A Device has been removed");
                            break;
                    
                    default:<<=========== This part gets underlined in blue with the message: Control cannot fall through from one case label to another.
                            MessageBox.Show("Nothing detected"); 
                            break;                  
    
                }
                base.WndProc(ref m);
            }//end wndProc
    Far as I can see, there's nothing wrong here. I may be overlooking something as I am tired at the moment
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    That exception is thrown if the label ("default" in this instance) does not terminate, however you have a break so I find it most bizarre that this is happening.

    For what it's worth, you don't strictly need to use the default label. You could terminate the function if one of your two labels are detected, or otherwise allow your default label code to occur outside of the switch. Example:

    Code:
            protected override void WndProc(ref Message m) 
            {
                switch (m.Msg)
                {
                    case DBT_DEVICEARRIVAL:
                        MessageBox.Show("A device has been docked");
                        return;
    
                    case DBT_DEVICEREMOVECOMPLETE:
                        MessageBox.Show("A Device has been removed");
                        return;
                }
    
                MessageBox.Show("Nothing detected");
                base.WndProc(ref m);
            }
    Last edited by theoobe; 04-08-2009 at 08:16 AM.

  3. #3
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271

    Question

    Thanks.
    I actually added the default (Contextually a logical error as it causes an infinite loop) to see if I could solve a previous error as it ws giving me the same error message with my last case statement. In any case I just rewrote my code the exact same way and it worked!!
    Yes even I'm puzzled. o_0?
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by WDT View Post
    Thanks.
    I actually added the default (Contextually a logical error as it causes an infinite loop) to see if I could solve a previous error as it ws giving me the same error message with my last case statement. In any case I just rewrote my code the exact same way and it worked!!
    Yes even I'm puzzled. o_0?
    I think some part of "exact" wasn't as exact as you wanted it to be - I've been there and done that MANY times.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch case statement
    By stanlvw in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 05:06 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM
  4. Strings and Switch Statement
    By Ajsan in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2004, 02:16 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21