views:

34

answers:

1

this outlines what i am trying to do.

this is not working for me, and it is unclear why.

thank you in advance for any and all help.

        Sub mySub()
        dim myDict as Dictionary
            myDict=new Dictionary

                myDict=myFunc()

        End Sub

        Function myFunc()
            dim myDict2
                set myDict2 = new Dictionary

                    'some code that does things and adds to myDict2'

            myFunc=myDict2
        End Function
+3  A: 

You'll need to use the SET keyword anytime you are assigning an object instead of a value:

    Sub mySub()
        dim myDict as Dictionary
        set myDict = myFunc()
    End Sub

    Function myFunc() as Dictionary
        dim myDict2 as Dictionary
        set myDict2 = new Dictionary
                'some code that does things and adds to myDict2'
        set myFunc=myDict2
    End Function

Your original code was also creating myDict as a new Dictionary object, then immediately replacing it with a different one. You can just skip that step.

BradC
this works with a slight alteration if you set myFunc(1)=myDict2. but then this creates an infinite loop.
jason m
No, its not an infinite loop. `myFunction = Value` (or `SET myFunction = Object`) is the VBA equivalent of `RETURN Value`
BradC
And you don't want `myFunc(1)=`, you just want `myFunc=`, otherwise you're just setting the first value of the dictionary to the entire other object, which isn't what you want.
BradC
yes the "Set myFunction=Obj" is the bit of code that was required. thank you very much for the pointer
jason m