pass by reference is default?
-
The help file says VB uses pass-by-reference as a default(byval to use pass by value) - but the simple test I do in an Excel module isn't working Public testnum ----- Public Sub makebig(inpt) MsgBox "Make big receives = " & inpt inpt = inpt + 1 MsgBox "Input now is = " & inpt MsgBox "testnum now is = " & testnum End Sub --- Public Sub test() testnum = 1 MsgBox "Before = " & testnum makebig (testnum) MsgBox "After = " & testnum End Sub test() does not change testnum, Before = 1, After = 1 what is happening? Thanks heaps
-
The help file says VB uses pass-by-reference as a default(byval to use pass by value) - but the simple test I do in an Excel module isn't working Public testnum ----- Public Sub makebig(inpt) MsgBox "Make big receives = " & inpt inpt = inpt + 1 MsgBox "Input now is = " & inpt MsgBox "testnum now is = " & testnum End Sub --- Public Sub test() testnum = 1 MsgBox "Before = " & testnum makebig (testnum) MsgBox "After = " & testnum End Sub test() does not change testnum, Before = 1, After = 1 what is happening? Thanks heaps
Change line that reads: makebig (testnum) to makebig testnum If you use parentheses here it assumes that parameter you pass is not a variable but a result of an expression. Therefore instead of passing address of your testnum variable it passes an address of the result of the expression.
-
The help file says VB uses pass-by-reference as a default(byval to use pass by value) - but the simple test I do in an Excel module isn't working Public testnum ----- Public Sub makebig(inpt) MsgBox "Make big receives = " & inpt inpt = inpt + 1 MsgBox "Input now is = " & inpt MsgBox "testnum now is = " & testnum End Sub --- Public Sub test() testnum = 1 MsgBox "Before = " & testnum makebig (testnum) MsgBox "After = " & testnum End Sub test() does not change testnum, Before = 1, After = 1 what is happening? Thanks heaps
Try this
Option Explicit
Public testnum As Integer
Public Sub MakeBig(inpt As Integer)
MsgBox "Make big receives 'inpt' = " & inpt, vbOKOnly, "In MakeBig()"
inpt = inpt + 1
MsgBox "'inpt' now is = " & inpt, vbOKOnly, "In MakeBig()"
MsgBox "testnum now is = " & testnum, vbOKOnly, "In MakeBig()"
End SubPublic Sub Test()
testnum = 1
MsgBox "The original value of testnum = " & testnum, vbOKOnly, "In Test()"
MakeBig testnum
MsgBox "After MakeBig()= " & testnum, vbOKOnly, "In Test"
End SubPaul Lyons Do not go where the path may lead, go instead where there is no path and leave a trail. - Ralph Waldo Emerson