When the requirement of modifying data passed to a function, setting the type of the object to a Class will always pass ByRef in visual basic, however, swapping to a “structure” will make such values read-only after setting and the values will be unassignable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
Module Module1 Public Class Myclass 'Change to structure and review the debug output for the difference. Public Test As Boolean Public Sub Disabled() Test = False End Sub End Class Public MyDictionary As New Dictionary(Of Int16, Myclass) Sub Main() Dim MyTempClass As New Myclass MyTempClass.Test = True For i = 0 To 10 MyDictionary.Add(i, MyTempClass) Next MyFunc(MyDictionary) For Each MyTest In MyDictionary MyTest.Value.Disabled() Next For i = 0 To MyDictionary.Count - 1 MyDictionary(i).Disabled() Next Dim MyNewClass = New Myclass MyNewClass.Test = True MyDictionary(2) = MyNewClass For Each MyTest In MyDictionary Debug.WriteLine(MyTest.Value.Test) Next End Sub Private Sub MyFunc(ByRef MyDic As Dictionary(Of Int16, Myclass)) For Each MyTest In MyDic MyTest.Value.Disabled() Next For Each MyTest In MyDic Debug.WriteLine(MyTest.Value.Test) Next End Sub End Module |