Announcement

Collapse
No announcement yet.

SELECT CASE inside a SELECT CASE?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • SELECT CASE inside a SELECT CASE?

    It does work, to begin with. But I'm pretty unsure if what I made is common practice. Therefore this question.

    My dialog has a number of button controls that have one task in common: they do change parts of the GUI. Apart from that, each of them has an own, smaller objective. Therefore I created a nested SELECT CASE under WM_COMMAND in the dialog's CALLBACK function, where the inner set processes the same controls, albeit one by one. Let's say the button ID's are 1001 through 1004, then this might be the pseudo-code:
    Code:
    SELECT CASE AS LONG CBCTL
      CASE 1001 TO 1004                                                          
        SELECT CASE AS LONG CBCTL
          CASE 1001
            ' do the typical 1001 job
          CASE 1002
            ' do the typical 1002 job
          CASE 1003
            ' ...
          CASE 1004
            ' ...
        END SELECT 
        ' now do the common 1001-1004 job
    END SELECT

    Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
    http://zijlema.basicguru.eu
    *** Opinions expressed here are not necessarily untrue ***

  • #2
    If it feels good, do it. There's really no difference between a SELECT..END SELECT inside a SELECT ... END-SELECT or an IF... ELSEIF ..ELSEIF..END IF inside the same SELECT..END SELECT. (The only other choice I can see).

    It's really not necessary, as you are making the same tests twice, but an additional couple of integer compares is a pretty small run-time price to pay for development-time maintainabilty.


    I mean it could just as easily be ...
    Code:
    SELECT CASE AS LONG CBCTL
          CASE 1001
            ' do the typical 1001 job
          CASE 1002
            ' do the typical 1002 job
          CASE 1003
            ' ...
          CASE 1004
            ' ...
        END SELECT 
    END SELECT
    SELECT CASE CBCTL 
       CASE 1001 TO 1004
          common job
    
    END SELECT
    .. which I personally would find a little easier to understand when I went back to it in six months, but it's essentially moot performance-wise and I'd just code for maintainability.

    MCM
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Doesn't work

      Michael, your approach does not work. It should be nested. Actually, one needs to mainselect them all and then, inside this selection, subselect one of 'm.

      What you try to do is to mainselect them twice, which is not possible.

      Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
      http://zijlema.basicguru.eu
      *** Opinions expressed here are not necessarily untrue ***

      Comment


      • #4
        Of course it doesn't work. I left an extra END SELECT in there when I cut and pasted. Just ignore the END SELECT which is not properly indented.

        >It should be nested
        Why?

        > mainselect them twice, which is not possible.

        Why not?

        Fix my typo and it is absolutely the same net result.
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          Proof of concept

          Code:
          ' Compiled with PBCC 4.04
          ' Will also compile with PBWIN 8.04
          #COMPILE EXE
          #DIM ALL
          DECLARE SUB Nested(lCBCTL AS LONG)
          DECLARE SUB Separate(lCBCTL AS LONG)
          '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          FUNCTION PBMAIN () AS LONG
              NESTED 1001
              NESTED 1002
              NESTED 1003
              NESTED 1004
              ? ""
              Separate 1001
              Separate 1002
              Separate 1003
              Separate 1004
              #IF %DEF(%PB_CC32)
                  WAITKEY$
              #ENDIF
          END FUNCTION
          '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          '   Subroutine to test the nested select statements.
          '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          SUB Nested(lCBCTL AS LONG)
              SELECT CASE AS LONG lCBCTL
                CASE 1001 TO 1004
                  SELECT CASE AS LONG lCBCTL
                    CASE 1001
                      ' do the typical 1001 job
                      ? "do the typical 1001 job"
                    CASE 1002
                      ' do the typical 1002 job
                      ? "do the typical 1002 job"
                    CASE 1003
                      ' ...
                      ? "do the typical 1003 job"
                    CASE 1004
                      ' ...
                      ? "do the typical 1004 job"
                  END SELECT
              ' now do the common 1001-1004 job
              ? "now do the common 1001-1004 job"
              END SELECT
          END SUB
           
          '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          '   Subroutine to test the separate select statements.
          '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          SUB Separate(lCBCTL AS LONG)
              SELECT CASE AS LONG lCBCTL
                CASE 1001
                  ' do the typical 1001 job
                  ? "do the typical 1001 job"
                CASE 1002
                  ' do the typical 1002 job
                  ? "do the typical 1002 job"
                CASE 1003
                  ' ...
                  ? "do the typical 1003 job"
                CASE 1004
                  ' ...
                  ? "do the typical 1004 job"
              END SELECT
              SELECT CASE lCBCTL
                 CASE 1001 TO 1004
                  ' now do the common 1001-1004 job
                  ? "now do the common 1001-1004 job"
              END SELECT
          END SUB
          '+++++++++++++++++++++++ end +++++++++++++++++++++++++++++++++++++++++++
          Results
          Code:
          do the typical 1001 job
          now do the common 1001-1004 job
          do the typical 1002 job
          now do the common 1001-1004 job
          do the typical 1003 job
          now do the common 1001-1004 job
          do the typical 1004 job
          now do the common 1001-1004 job
           
          do the typical 1001 job
          now do the common 1001-1004 job
          do the typical 1002 job
          now do the common 1001-1004 job
          do the typical 1003 job
          now do the common 1001-1004 job
          do the typical 1004 job
          now do the common 1001-1004 job
          Regards,
          Bob

          Comment


          • #6
            Did not test it again

            To Michael:
            I did not test it again, Mike. I really believe that your approach will work, but further thinking leads me to the conclusion that my approach contains more logic. If you want a candy from a box (read: a number of buttons with a common task) you need the filled box in the first place in order to be able to pick a candy from it (read: one of the buttons with its specific task). For the case of better readability of the code I now think of using IF/END IF:
            Code:
            SELECT CASE AS LONG CBCTL
              CASE 1001 TO 1004           
                IF CBCTL = 
                ' do specific task
                END IF 
                ' and after the final IF - END IF:
                ' do common tasks
              CASE %ID_WHATEVER_OTHER_CONTROL
                ' etcetera   
            END SELECT
            To Robert:
            Yes, exactly what I have. I only wanted to know if that is common practice. Thanks anyway for your effort.

            Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
            http://zijlema.basicguru.eu
            *** Opinions expressed here are not necessarily untrue ***

            Comment


            • #7
              I use a Select case in a Select case all the time. In fact some USB code I am working on, I have to parse a string that many have a common beginning, but then the format of the rest of the string is different so I do something like the pseudo-code below

              Code:
                   SELECT CASE INSTR(UsbInterfaceInstance, "USB#")
                        CASE 0
                        CASE ELSE
              'Do something here
                             SELECT CASE INSTR(UsbInterfaceInstance, "ROOT_HUB#")
                                  CASE 0
                                  CASE ELSE
              'Do something specific here
                                       END SELECT
                             END SELECT
                             SELECT CASE INSTR(UsbInterfaceInstance, "ROOT_HUB20#")
                                  CASE 0
                                  CASE ELSE
              'Do something specific here
                                       END SELECT
                             END SELECT
                   END SELECT
              For clarity I would comment the differences in each case, but they all start out the same
              Engineer's Motto: If it aint broke take it apart and fix it

              "If at 1st you don't succeed... call it version 1.0"

              "Half of Programming is coding"....."The other 90% is DEBUGGING"

              "Document my code????" .... "WHYYY??? do you think they call it CODE? "

              Comment

              Working...
              X