-
I understand the concepts of by reference and by value, but has anyone else encountered a problem with passing a variable by reference and then that variable being reset for apparently no reason?
I called something like this:
Code:
subroutine(ByRef oIndex as integer)
And it would set oIndex to 0 after it completed, but it would always have the proper value all the way through the sub routine. Setting oIndex to ByVal solved the problem no sweat, but I was wondering if anyone knows why this happened. It did it on several subroutines.
The Routines only used oIndex as an index for a class/struct, and were used very rarely throughout the subroutine.
-
I'm not sure what it is you are saying happened, but (and you may know this) when you call a subroutine with a ByRef variable, you are actually calling the subroutine with the address of that variable, so the subroutine has the potential to change the value of the variable. So in the following example the MsgBox will display 55 and not 6.
Code:
Private Sub Form_Load()
Dim MyIndex As Integer
MyIndex = 6
subroutine MyIndex
MsgBox MyIndex
End Sub
Public Sub subroutine(ByRef oIndex As Integer)
oIndex = 55
End Sub
-
Have you tried put a break on the last line in your subroutine, if your oIndex is 0 you could probably track the change somewhere, otherways there might be another problem, ie, if youre passing a property of an object or a read only value