Jump to content

Need help with Assign & Eval/Execute


Recommended Posts

I know that using Assign & Eval are frowned upon (one poster insisted that they're only for lazy losers), but it seems to me that if I could get them to work properly, they're perfect for what I'm trying to do.  And that is to try to reduce redundant code while initializing some repetitively-named arrays.  For example, instead of this:

Func _MyInitContextMenus()

    Const $Cn_CtxMenuItemItemReplStrDef     = "Alpha"
    Const $Cn_CtxMenuItemShoFldNameStrDef   = "Beta"
    Const $Cn_CtxMenuItemNOpnFldStrDef      = "Delta"
    Const $Cn_CtxMenuItemItemResetStrDef    = "Gamma"

    Global $G_SlotBtnCtxMenuID1 = GUICtrlCreateContextMenu( $Gc_ItemBtnID1 )
    Global $G_SlotBtnCtxMenuID2 = GUICtrlCreateContextMenu( $Gc_ItemBtnID2 )
    Global $G_SlotBtnCtxMenuID3 = GUICtrlCreateContextMenu( $Gc_ItemBtnID3 )
    Global $G_SlotBtnCtxMenuID4 = GUICtrlCreateContextMenu( $Gc_ItemBtnID4 )
    Global $G_SlotBtnCtxMenuID5 = GUICtrlCreateContextMenu( $Gc_ItemBtnID5 )
    Global $G_SlotBtnCtxMenuID6 = GUICtrlCreateContextMenu( $Gc_ItemBtnID6 )
    Global $G_SlotBtnCtxMenuID7 = GUICtrlCreateContextMenu( $Gc_ItemBtnID7 )
    Global $G_SlotBtnCtxMenuID8 = GUICtrlCreateContextMenu( $Gc_ItemBtnID8 )
;
    $G_CtxMenuInfoAra[1][$Cn_CtxMenuInfoAraMainCtxIdIx] = $G_SlotBtnCtxMenuID1
    $G_CtxMenuInfoAra[2][$Cn_CtxMenuInfoAraMainCtxIdIx] = $G_SlotBtnCtxMenuID2
    $G_CtxMenuInfoAra[3][$Cn_CtxMenuInfoAraMainCtxIdIx] = $G_SlotBtnCtxMenuID3
    $G_CtxMenuInfoAra[4][$Cn_CtxMenuInfoAraMainCtxIdIx] = $G_SlotBtnCtxMenuID4
    $G_CtxMenuInfoAra[5][$Cn_CtxMenuInfoAraMainCtxIdIx] = $G_SlotBtnCtxMenuID5
    $G_CtxMenuInfoAra[6][$Cn_CtxMenuInfoAraMainCtxIdIx] = $G_SlotBtnCtxMenuID6
    $G_CtxMenuInfoAra[7][$Cn_CtxMenuInfoAraMainCtxIdIx] = $G_SlotBtnCtxMenuID7
    $G_CtxMenuInfoAra[8][$Cn_CtxMenuInfoAraMainCtxIdIx] = $G_SlotBtnCtxMenuID8

    For $i = 1 To $G_CtxMenuInfoAraNumRows
        GUICtrlSetOnEvent( $G_CtxMenuInfoAra[$i][$Cn_CtxMenuInfoAraMainCtxIdIx], "ContextClick" & $i )
    Next

    Local $Lf_MenuItemReplItemID1 = GUICtrlCreateMenuItem($Cn_CtxMenuItemItemReplStrDef, $G_SlotBtnCtxMenuID1 )
    Local $Lf_MenuItemShowFinalFolderID1 = GUICtrlCreateMenuItem($Cn_CtxMenuItemShoFldNameStrDef, $G_SlotBtnCtxMenuID1 )
    Local $Lf_MenuItemROpenFolderID1 = GUICtrlCreateMenuItem($Cn_CtxMenuItemNOpnFldStrDef, $G_SlotBtnCtxMenuID1 )
    Local $Lf_MenuItemResetSlotID1 = GUICtrlCreateMenuItem($Cn_CtxMenuItemItemResetStrDef, $G_SlotBtnCtxMenuID1 )
    Local $Lf_MenuItemEmbAra[$Cn_MenuItemEmbAraNumElems] = [ $Lf_MenuItemReplItemID1, $Lf_MenuItemShowFinalFolderID1, _
                                                             $Lf_MenuItemROpenFolderID1, $Lf_MenuItemResetSlotID1 ]
    $G_CtxMenuInfoAra[1][$Cn_CtxMenuInfoAraItemAraIx] = $Lf_MenuItemEmbAra

    Local $Lf_MenuItemReplItemID2 = GUICtrlCreateMenuItem($Cn_CtxMenuItemItemReplStrDef, $G_SlotBtnCtxMenuID2 )
    Local $Lf_MenuItemShowFinalFolderID2 = GUICtrlCreateMenuItem($Cn_CtxMenuItemShoFldNameStrDef, $G_SlotBtnCtxMenuID2 )
    Local $Lf_MenuItemROpenFolderID2 = GUICtrlCreateMenuItem($Cn_CtxMenuItemNOpnFldStrDef, $G_SlotBtnCtxMenuID2 )
    Local $Lf_MenuItemResetSlotID2 = GUICtrlCreateMenuItem($Cn_CtxMenuItemItemResetStrDef, $G_SlotBtnCtxMenuID2 )
    Local $Lf_MenuItemEmbAra[$Cn_MenuItemEmbAraNumElems] = [ $Lf_MenuItemReplItemID2, $Lf_MenuItemShowFinalFolderID2, _
                                                             $Lf_MenuItemROpenFolderID2, $Lf_MenuItemResetSlotID2 ]
    $G_CtxMenuInfoAra[2][$Cn_CtxMenuInfoAraItemAraIx] = $Lf_MenuItemEmbAra

    Local $Lf_MenuItemReplItemID3 = GUICtrlCreateMenuItem($Cn_CtxMenuItemItemReplStrDef, $G_SlotBtnCtxMenuID3 )
    Local $Lf_MenuItemShowFinalFolderID3 = GUICtrlCreateMenuItem($Cn_CtxMenuItemShoFldNameStrDef, $G_SlotBtnCtxMenuID3 )
    Local $Lf_MenuItemROpenFolderID3 = GUICtrlCreateMenuItem($Cn_CtxMenuItemNOpnFldStrDef, $G_SlotBtnCtxMenuID3 )
    Local $Lf_MenuItemResetSlotID3 = GUICtrlCreateMenuItem($Cn_CtxMenuItemItemResetStrDef, $G_SlotBtnCtxMenuID3 )
    Local $Lf_MenuItemEmbAra[$Cn_MenuItemEmbAraNumElems] = [ $Lf_MenuItemReplItemID3, $Lf_MenuItemShowFinalFolderID3, _
                                                             $Lf_MenuItemROpenFolderID3, $Lf_MenuItemResetSlotID3 ]
    $G_CtxMenuInfoAra[3][$Cn_CtxMenuInfoAraItemAraIx] = $Lf_MenuItemEmbAra

    Local $Lf_MenuItemReplItemID4 = GUICtrlCreateMenuItem($Cn_CtxMenuItemItemReplStrDef, $G_SlotBtnCtxMenuID4 )
    Local $Lf_MenuItemShowFinalFolderID4 = GUICtrlCreateMenuItem($Cn_CtxMenuItemShoFldNameStrDef, $G_SlotBtnCtxMenuID4 )
    Local $Lf_MenuItemROpenFolderID4 = GUICtrlCreateMenuItem($Cn_CtxMenuItemNOpnFldStrDef, $G_SlotBtnCtxMenuID4 )
    Local $Lf_MenuItemResetSlotID4 = GUICtrlCreateMenuItem($Cn_CtxMenuItemItemResetStrDef, $G_SlotBtnCtxMenuID4 )
    Local $Lf_MenuItemEmbAra[$Cn_MenuItemEmbAraNumElems] = [ $Lf_MenuItemReplItemID4, $Lf_MenuItemShowFinalFolderID4, _
                                                             $Lf_MenuItemROpenFolderID4, $Lf_MenuItemResetSlotID4 ]
    $G_CtxMenuInfoAra[4][$Cn_CtxMenuInfoAraItemAraIx] = $Lf_MenuItemEmbAra

    Local $Lf_MenuItemReplItemID5 = GUICtrlCreateMenuItem($Cn_CtxMenuItemItemReplStrDef, $G_SlotBtnCtxMenuID5 )
    Local $Lf_MenuItemShowFinalFolderID5 = GUICtrlCreateMenuItem($Cn_CtxMenuItemShoFldNameStrDef, $G_SlotBtnCtxMenuID5 )
    Local $Lf_MenuItemROpenFolderID5 = GUICtrlCreateMenuItem($Cn_CtxMenuItemNOpnFldStrDef, $G_SlotBtnCtxMenuID5 )
    Local $Lf_MenuItemResetSlotID5 = GUICtrlCreateMenuItem($Cn_CtxMenuItemItemResetStrDef, $G_SlotBtnCtxMenuID5 )
    Local $Lf_MenuItemEmbAra[$Cn_MenuItemEmbAraNumElems] = [ $Lf_MenuItemReplItemID5, $Lf_MenuItemShowFinalFolderID5, _
                                                             $Lf_MenuItemROpenFolderID5, $Lf_MenuItemResetSlotID5 ]
    $G_CtxMenuInfoAra[5][$Cn_CtxMenuInfoAraItemAraIx] = $Lf_MenuItemEmbAra

    Local $Lf_MenuItemReplItemID6 = GUICtrlCreateMenuItem($Cn_CtxMenuItemItemReplStrDef, $G_SlotBtnCtxMenuID6 )
    Local $Lf_MenuItemShowFinalFolderID6 = GUICtrlCreateMenuItem($Cn_CtxMenuItemShoFldNameStrDef, $G_SlotBtnCtxMenuID6 )
    Local $Lf_MenuItemROpenFolderID6 = GUICtrlCreateMenuItem($Cn_CtxMenuItemNOpnFldStrDef, $G_SlotBtnCtxMenuID6 )
    Local $Lf_MenuItemResetSlotID6 = GUICtrlCreateMenuItem($Cn_CtxMenuItemItemResetStrDef, $G_SlotBtnCtxMenuID6 )
    Local $Lf_MenuItemEmbAra[$Cn_MenuItemEmbAraNumElems] = [ $Lf_MenuItemReplItemID6, $Lf_MenuItemShowFinalFolderID6, _
                                                             $Lf_MenuItemROpenFolderID6, $Lf_MenuItemResetSlotID6 ]
    $G_CtxMenuInfoAra[6][$Cn_CtxMenuInfoAraItemAraIx] = $Lf_MenuItemEmbAra

    Local $Lf_MenuItemReplItemID7 = GUICtrlCreateMenuItem($Cn_CtxMenuItemItemReplStrDef, $G_SlotBtnCtxMenuID7 )
    Local $Lf_MenuItemShowFinalFolderID7 = GUICtrlCreateMenuItem($Cn_CtxMenuItemShoFldNameStrDef, $G_SlotBtnCtxMenuID7 )
    Local $Lf_MenuItemROpenFolderID7 = GUICtrlCreateMenuItem($Cn_CtxMenuItemNOpnFldStrDef, $G_SlotBtnCtxMenuID7 )
    Local $Lf_MenuItemResetSlotID7 = GUICtrlCreateMenuItem($Cn_CtxMenuItemItemResetStrDef, $G_SlotBtnCtxMenuID7 )
    Local $Lf_MenuItemEmbAra[$Cn_MenuItemEmbAraNumElems] = [ $Lf_MenuItemReplItemID7, $Lf_MenuItemShowFinalFolderID7, _
                                                             $Lf_MenuItemROpenFolderID7, $Lf_MenuItemResetSlotID7 ]
    $G_CtxMenuInfoAra[7][$Cn_CtxMenuInfoAraItemAraIx] = $Lf_MenuItemEmbAra

    Local $Lf_MenuItemReplItemID8 = GUICtrlCreateMenuItem($Cn_CtxMenuItemItemReplStrDef, $G_SlotBtnCtxMenuID8 )
    Local $Lf_MenuItemShowFinalFolderID8 = GUICtrlCreateMenuItem($Cn_CtxMenuItemShoFldNameStrDef, $G_SlotBtnCtxMenuID8 )
    Local $Lf_MenuItemROpenFolderID8 = GUICtrlCreateMenuItem($Cn_CtxMenuItemNOpnFldStrDef, $G_SlotBtnCtxMenuID8 )
    Local $Lf_MenuItemResetSlotID8 = GUICtrlCreateMenuItem($Cn_CtxMenuItemItemResetStrDef, $G_SlotBtnCtxMenuID8 )
    Local $Lf_MenuItemEmbAra[$Cn_MenuItemEmbAraNumElems] = [ $Lf_MenuItemReplItemID8, $Lf_MenuItemShowFinalFolderID8, _
                                                             $Lf_MenuItemROpenFolderID8, $Lf_MenuItemResetSlotID8 ]
    $G_CtxMenuInfoAra[8][$Cn_CtxMenuInfoAraItemAraIx] = $Lf_MenuItemEmbAra

    Return
EndFunc

 

I'd like to reduce the redundancy with something more like this:

Func _MyInitContextMenus()
;
    Local $Lf_CtxMenuCtlID, $Lf_CtxMenuItemID
;
    Const $Cn_CtxMenuItemItemReplStrDef     = "Alpha"
    Const $Cn_CtxMenuItemShoFldNameStrDef   = "Beta"
    Const $Cn_CtxMenuItemNOpnFldStrDef      = "Delta"
    Const $Cn_CtxMenuItemItemResetStrDef    = "Gamma"

    Global $G_SlotBtnCtxMenuID1 = GUICtrlCreateContextMenu( $Gc_ItemBtnID1 )
    Global $G_SlotBtnCtxMenuID2 = GUICtrlCreateContextMenu( $Gc_ItemBtnID2 )
    Global $G_SlotBtnCtxMenuID3 = GUICtrlCreateContextMenu( $Gc_ItemBtnID3 )
    Global $G_SlotBtnCtxMenuID4 = GUICtrlCreateContextMenu( $Gc_ItemBtnID4 )
    Global $G_SlotBtnCtxMenuID5 = GUICtrlCreateContextMenu( $Gc_ItemBtnID5 )
    Global $G_SlotBtnCtxMenuID6 = GUICtrlCreateContextMenu( $Gc_ItemBtnID6 )
    Global $G_SlotBtnCtxMenuID7 = GUICtrlCreateContextMenu( $Gc_ItemBtnID7 )
    Global $G_SlotBtnCtxMenuID8 = GUICtrlCreateContextMenu( $Gc_ItemBtnID8 )
;
    $G_CtxMenuInfoAra[1][$Cn_CtxMenuInfoAraMainCtxIdIx] = $G_SlotBtnCtxMenuID1
    $G_CtxMenuInfoAra[2][$Cn_CtxMenuInfoAraMainCtxIdIx] = $G_SlotBtnCtxMenuID2
    $G_CtxMenuInfoAra[3][$Cn_CtxMenuInfoAraMainCtxIdIx] = $G_SlotBtnCtxMenuID3
    $G_CtxMenuInfoAra[4][$Cn_CtxMenuInfoAraMainCtxIdIx] = $G_SlotBtnCtxMenuID4
    $G_CtxMenuInfoAra[5][$Cn_CtxMenuInfoAraMainCtxIdIx] = $G_SlotBtnCtxMenuID5
    $G_CtxMenuInfoAra[6][$Cn_CtxMenuInfoAraMainCtxIdIx] = $G_SlotBtnCtxMenuID6
    $G_CtxMenuInfoAra[7][$Cn_CtxMenuInfoAraMainCtxIdIx] = $G_SlotBtnCtxMenuID7
    $G_CtxMenuInfoAra[8][$Cn_CtxMenuInfoAraMainCtxIdIx] = $G_SlotBtnCtxMenuID8
;
;   Note: Can't use Assign/Eval on this next block because they can't be used for function names
;
    GUICtrlSetOnEvent( $G_SlotBtnCtxMenuID1, "ContextClick1" )
    GUICtrlSetOnEvent( $G_SlotBtnCtxMenuID2, "ContextClick2" )
    GUICtrlSetOnEvent( $G_SlotBtnCtxMenuID3, "ContextClick3" )
    GUICtrlSetOnEvent( $G_SlotBtnCtxMenuID4, "ContextClick4" )
    GUICtrlSetOnEvent( $G_SlotBtnCtxMenuID5, "ContextClick5" )
    GUICtrlSetOnEvent( $G_SlotBtnCtxMenuID6, "ContextClick6" )
    GUICtrlSetOnEvent( $G_SlotBtnCtxMenuID7, "ContextClick7" )
    GUICtrlSetOnEvent( $G_SlotBtnCtxMenuID8, "ContextClick8" )

    For $i = 1 To $G_CtxMenuInfoAraNumRows                              ; For each row in $G_CtxMenuInfoAra[]
        $Lf_CtxMenuCtlID = $G_CtxMenuInfoAra[$i][$Cn_CtxMenuInfoAraMainCtxIdIx]
        GUICtrlSetState( $Lf_CtxMenuCtlID, $GUI_DISABLE )               ; Don't enable the context menu until populated
    Next
;
    Assign( "$G_AsgSlotBtnCtxMenuID1", String($G_SlotBtnCtxMenuID1), $ASSIGN_FORCEGLOBAL )
    Assign( "$G_AsgSlotBtnCtxMenuID2", String($G_SlotBtnCtxMenuID2), $ASSIGN_FORCEGLOBAL )
    Assign( "$G_AsgSlotBtnCtxMenuID3", String($G_SlotBtnCtxMenuID3), $ASSIGN_FORCEGLOBAL )
    Assign( "$G_AsgSlotBtnCtxMenuID4", String($G_SlotBtnCtxMenuID4), $ASSIGN_FORCEGLOBAL )
    Assign( "$G_AsgSlotBtnCtxMenuID5", String($G_SlotBtnCtxMenuID5), $ASSIGN_FORCEGLOBAL )
    Assign( "$G_AsgSlotBtnCtxMenuID6", String($G_SlotBtnCtxMenuID6), $ASSIGN_FORCEGLOBAL )
    Assign( "$G_AsgSlotBtnCtxMenuID7", String($G_SlotBtnCtxMenuID7), $ASSIGN_FORCEGLOBAL )
    Assign( "$G_AsgSlotBtnCtxMenuID8", String($G_SlotBtnCtxMenuID8), $ASSIGN_FORCEGLOBAL )
;
;   Debugging display of Assign results
;
    $Lf_Text = "Display of Context Menu IDs in $G_CtxMenuInfoAra[]..."
    For $i = 1 To $G_CtxMenuInfoAraNumRows -1
        $Lf_Text &= @CRLF & "$G_CtxMenuInfoAra[" & $i & "] = " & $G_CtxMenuInfoAra[$i][$Cn_CtxMenuInfoAraMainCtxIdIx]
    Next
    ClipPut( $Lf_Text )
    MsgBox($MB_OK, "Array Values", $Lf_Text)    ; <<=== These results are correct
;
    $Lf_Text = "Display of Assigned $G_AsgSlotBtnCtxMenu[] Results..."
    For $i = 1 To $G_CtxMenuInfoAraNumRows -1
        Local $Lf_EvalRes = Number( Eval("$G_AsgSlotBtnCtxMenuID" & $i) )
        Local $Lf_ExecRes = Number( Execute("$G_AsgSlotBtnCtxMenuID" & $i) )
        $Lf_Text &= @CRLF & "Eval of $G_AsgSlotBtnCtxMenuID" & $i & ' = "' & $Lf_EvalRes & '", Exec = "' & $Lf_ExecRes & '"'
    Next
    ClipPut( $Lf_Text )
    MsgBox($MB_OK, "Assign Results", $Lf_Text)  ; <<=== These are all "0" due to null strings from Eval/Execute
;
;   End of debugging section
;

    For $i = 1 To $Cn_MenuItemEmbAraNumElems
        Assign( ("$Lf_MenuItemReplItemID" & $i), _
                    String( GUICtrlCreateMenuItem($Cn_CtxMenuItemItemReplStrDef, $G_CtxMenuInfoAra[$i][$Cn_CtxMenuInfoAraMainCtxIdIx]), _
                     $ASSIGN_FORCELOCAL) )

        Assign( ("$Lf_MenuItemShowFinalFolderID" & $i), _
                    String( GUICtrlCreateMenuItem($Cn_CtxMenuItemShoFldNameStrDef, $G_CtxMenuInfoAra[$i][$Cn_CtxMenuInfoAraMainCtxIdIx]), _
                     $ASSIGN_FORCELOCAL) )

        Assign( ("$Lf_MenuItemROpenFolderID" & $i), _
                    String( GUICtrlCreateMenuItem($Cn_CtxMenuItemNOpnFldStrDef, $G_CtxMenuInfoAra[$i][$Cn_CtxMenuInfoAraMainCtxIdIx]), _
                     $ASSIGN_FORCELOCAL) )

        Assign( ("$Lf_MenuItemResetSlotID" & $i), _
                    String( GUICtrlCreateMenuItem($Cn_CtxMenuItemItemResetStrDef, $G_CtxMenuInfoAra[$i][$Cn_CtxMenuInfoAraMainCtxIdIx]), _
                     $ASSIGN_FORCELOCAL) )

        Local  $Lf_MenuItemEmbAra[$Cn_MenuItemEmbAraNumElems] = [ Eval("$Lf_MenuItemReplItemID" & $i), _
                                                           Eval("$Lf_MenuItemShowFinalFolderID" & $i), _
                                                           Eval("$Lf_MenuItemROpenFolderID" & $i), _
                                                           Eval("$Lf_MenuItemResetSlotID" & $i) ]

        $G_CtxMenuInfoAra[$i][$Cn_CtxMenuInfoAraItemAraIx] = $Lf_MenuItemEmbAra

    Next
;
    For $i = 1 To $G_CtxMenuInfoAraNumRows
        For $j = 0 To $Cn_MenuItemEmbAraNumElems -1
            $Lf_CtxMenuItemID = ($G_CtxMenuInfoAra[$i][$Cn_CtxMenuInfoAraItemAraIx])[$j]
            GUICtrlSetState( $Lf_CtxMenuItemID, $GUI_DISABLE )
        Next
    Next
;
    Return
EndFunc   ;==>_MyInitContextMenus

The problem is, this doesn't work!  As indicated, both the Eval and Execute results are always a null string!

Now, I'm not wedded to using Assign/Eval, so if there's some good alternative to just writing it all out redundantly, please let me know.  Otherwise, would you be so kind as to instruct me as to what I'm doing wrong?

Thanks!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...