2

I have a class State and some sub inside it that takes a Scripting.Dictionary as an argument. However when I try to pass a dictionary there, I get a wrong number of arguments or invalid property assignment error. I can't figure out what's wrong.

'Sub insite State class
Sub addSecondItems(itemsDict As Object)
    MsgBox ("start addSecondItems")
End Sub

Sub test()
Dim stateCopy As State
Set stateCopy = New State
...
Dim dict1 As Object
Set dict1 = CreateObject("Scripting.Dictionary")
stateCopy.addSecondItems (dict1)  'error here
...
End Sub

At the same time

Sub testPetDict()

    Dim petDict As Object
    Set petDict = CreateObject("Scripting.Dictionary")
    Call readPetDict(petDict)

End Sub

Sub readPetDict(petDict As Object)
        Dim year As Integer
        For year = 2014 To 2017
            MsgBox (year & ". " & petDict(year))
        Next
End Sub

works fine.

What may be wrong here and why the second case works, while the first fails?

6
  • 3
    Remove the brackets: stateCopy.addSecondItems dict1 or use Call Commented Sep 19, 2017 at 13:29
  • 2
    By using parentheses, you force the object to be passed by value, hence the error. Commented Sep 19, 2017 at 13:36
  • @Rory Thank you kind person! You saved me from going irreversible desperate! You might post an answer and I'll approve it. Commented Sep 19, 2017 at 13:56
  • @Kostas What's wrong with a dictionary being passed by value though? It's not like I need to edit it or something. Commented Sep 19, 2017 at 13:57
  • 1
    You can't pass an object by value, only by reference. Passing by value means creating and passing a copy where by reference, only a reference to the object is passed. Commented Sep 19, 2017 at 13:58

1 Answer 1

3

You should remove the brackets:

stateCopy.addSecondItems dict1

or use Call

Call stateCopy.addSecondItems(dict1)

Otherwise the brackets try to coerce the dictionary to a value by calling its default property, Item, which requires an argument, hence the error message.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.