Development‎ > ‎VBScript‎ > ‎

Passing Dictionary object as the function parameter

When dictionary object is passed as the parameter into Subroutine or function it is always passed by reference. This statement is true even if the byValue proced the variable name in the function signature.

Following code demonstrates that:
'Subroutine which takes dictionary object as
'the parameter and adds one element to it.
Sub AddItemToDictionary(byVal dicDictionaryParam)
      'Add item to the dictionary object passed in as parameter
    dicDictionaryParam.Add "NewElement", "ItemValue"
End Sub
 
'Create dictionary object
Dim dicDictionary : Set dicDictionary = CreateObject("scripting.dictionary")
 
'Display message box with number of items in the dictionary
'Output: Number of elements in the dictionary: 0
MsgBox "Number of elements in the dictionary: " & dicDictionary.Count
 
'Call the function to add item into the dictionary
AddItemToDictionary dicDictionary
 
'Display message box with number of items in the dictionary
'Output: Number of elements in the dictionary: 1
MsgBox "Number of elements in the dictionary: " & dicDictionary.Count

Comments