Thread: Nesting switchstatments

  1. #1
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104

    Nesting switchstatments

    I am using a swtichstatment in the Windowsprocedure, to check the WM_values. But when it comes to WM_COMMAND, for instance, it requires another switch statement.

    I think nested switch-statments like that are messy and ugly...
    Do anyone know of a good alternative?

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Check out function pointer.

    Kuphryn

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Do anyone know of a good alternative?
    Uh, if-then-else?

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I think nested switch is ok and somewhat expected when using the win32 api
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I usually split the command handler into a separate function (here called MenuHandler since I often use WM_COMMAND for menu actions):
    Code:
    LRESULT MenuHandler(WORD MenuItem)
    {
      switch(MenuItem)
      {
        case FileExit:
        {
          PostQuitMessage(0);
          return 0;
        }
    
        ...
      }
    
      return 0;
    }
    
    LRESULT MessageHandler(HWND Window, UINT Message, WPARAM W, LPARAM L)
    {
      switch(Message)
      {
        case WM_COMMAND:
        {
          return MenuHandler(LOWORD(W));
        }
    
        ...
      }
    
      return DefWindowProc(Window, Message, W, L);
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Do anyone know of a good alternative?
    Yeah, call a function.

    Code:
    void bar ( ) {
      switch ( something ) {
      }
    }
    
    void foo ( ) {
      switch ( something_else ) {
        case a_thing: bar(); break;
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mice Are Nesting!!!
    By Dagger in forum C++ Programming
    Replies: 4
    Last Post: 02-05-2006, 11:59 PM
  2. Nesting
    By coo_pal in forum C Programming
    Replies: 1
    Last Post: 01-27-2003, 11:16 PM
  3. Nesting Parentheses
    By XZSNPP in forum C++ Programming
    Replies: 6
    Last Post: 01-18-2003, 10:01 PM
  4. DLLs <- sound files, and nesting.
    By sean in forum C++ Programming
    Replies: 2
    Last Post: 10-28-2002, 05:13 PM
  5. #include nesting level?
    By Josho in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2001, 12:28 AM