You can also use ByRef or ByVal values ββin a function. ByVal, the object you sent the function or sub to is copied to a private memmory for use inside the function and discarded after the function finishes. ByRef, the object to which you sent the function refers to all the manipulations you made, deleting keys, setting the object, etc. It is directly done for the object you sent.
eg.
Sub test DIM testDict as variant call setdict(testDict) testDict.Add "test", "value" call addValue(testDict, "test2","another value") msgbox testDict.Count Set testDict = Nothing End Sub Sub setdict(ByRef in_Dict as Variant) If Typename(in_Dict) <> "Dictionary" Then SET in_Dict = CreateObject("Scripting.Dictionary") end if end sub sub addValue(ByRef in_Obj as Variant, ByVal in_Key as String, ByVal in_Value as String) if not in_Obj.Exists(in_Key) then in_Obj.Add in_Key, in_Value end if end sub
Test subheadings with a variable type variant to a subset. In the function, I check the type of object sent to sub. If the type of the object is not a dictionary object (which it is not), then the in_Dict object, which is actually the testDict object declared in the test, will be installed on the dictionary object.
To demonstrate the link better, I also included the second addvalue subtask. I pass the object again to the function as a reference and add another key to the dictionary object. In the main test sub-selection, then submit the score. In this case, there are 2 keys.
Werner de jong
source share