Announcement

Collapse
No announcement yet.

Minimal Callback Not Used

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

  • Minimal Callback Not Used

    Trivial comment: Even though I like minimal code, I do this ...
    Code:
    CallBack Function DlgProc() As Long
       Select Case Cb.Msg
          Case %WM_Command
             Select Case Cb.Ctl
                Case %IDC_DoThis : DoThis
             End Select
       End Select
    End Function
    ... instead of this, even when the code is for a quick test using throwaway code.
    Code:
    CallBack Function DlgProc() As Long
       If Cb.Msg = %WM_Command And Cb.Ctl = %IDC_DoThis Then DoThis
    End Function

  • #2
    I don't see anything wrong with it as long as you don't care which message is coming from control %IDC_DoThis. Could be a click or a hover or ...

    You could make it
    Code:
    If (Cb.Msg = %WM_Command) And (Cb.Ctl = %IDC_DoThis) And (Cb.CtlMsg = %BN_Clicked) Then DoThis
    but you would need a line for each message and each control.

    Comment


    • #3
      Agree.

      IF/THEN is just as legit a test as SELECT CASE.

      It depends on how many messages, controls and sub-messages need testing.

      For one CB.MSG (like %WM_COMMAND) and multiple controls I might use an IF THEN/END IF with a SELECT CASE nested inside it.

      Even for a complicated callback, it is possible to use only nested IF THEN/ ELSEIFs/END IFs. It would just be "ugly" to read compared to nested SELECT CASEs; but the tests of the values would be valid (no coding errors of course!)

      There is no magic saying callbacks must use SELECT CASE. SELECT CASE just often is easier to write and read/modify later.

      Cheers,
      Dale

      Comment


      • #4
        Me too. It's clear and easily expandable.

        And since that structure is automagically created for me when I select "New File As... " and pick "DDT-SImple Dlg" off my list of 10 templates, it's actually less work that writing one or more multi-conditional IF statements.

        Code:
        2
        CCWIN
        .bas
        [PBWIN]DDT-Simple Dlg
        
        #COMPILE EXE
        #DIM ALL
        ENUM ctrls SINGULAR
            IDC_btnQuit = 1001
        END ENUM
        
        FUNCTION PBMAIN() AS LONG
           LOCAL lRslt AS LONG
           LOCAL hDlg  AS DWORD
        
            DIALOG NEW 0, "DDT SHell", , , 200, 180, %WS_SYSMENU, TO hDlg
            CONTROL ADD BUTTON , hDlg,%IDC_btnQuit,"Quit",60,130,80,20
            DIALOG SHOW MODAL hDlg, CALL MainDlgCB TO lRslt
        END FUNCTION
        
        CALLBACK FUNCTION MainDlgCB()
            SELECT CASE AS LONG CB.MSG
                CASE %WM_INITDIALOG
        
                CASE %WM_NCACTIVATE
        
                CASE %WM_COMMAND
                    SELECT CASE AS LONG CB.CTL
                        CASE %IDC_btnQUIT
                            IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN
                                DIALOG END CBHNDL
                            END IF
                    END SELECT
            END SELECT
        END FUNCTION

        Comment


        • #5
          Please note "SELECT CASE AS LONG" is NOT the same test as "IF/ELSE" if you are testing a 32-bit integer with PowerBASIC code.

          SELECT CASE AS LONG tests all 32 bits,(unsigned) and IF/ELSE tests 31 bits plus a sign bit.

          e.g., using SELECT CASE AS AS LONG , -1& = &FFFFFFFF but that is not true using IF/ELSE

          I mention this because the code example in post #4 uses SELECT CASE AS LONG.
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment

          Working...
          X