ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   ByRef not passing address (https://www.excelbanter.com/excel-programming/281023-byref-not-passing-address.html)

Ian Stanborough

ByRef not passing address
 
Hope some one can help

My problem is when I try to create a function that will increment an
integer, the calling parameter I believe is passed as a value and not a
reference. The help file suggest that default value of parameters passed is
ByRef, but this example tell me in the case of integer its not.

Does any one have a work around.

My email address is
All suggests are welcomed. A coded example would explain all


Program
dim myValue as integer
.....
myValue = 0
increment(myValue)
' here myValue still has value of 0, because it was not passed to
subroutine as reference and hence not incremented
....
end Program



sub increment( number as integer)
number = number + 1
end sub



Jim Rech

ByRef not passing address
 
This shows passing by ref is in affect:

Sub One()
Dim X As Integer
Two X
MsgBox X
End Sub

Sub Two(Y As Integer)
Y = Y + 1
End Sub


--
Jim Rech
Excel MVP



Chip Pearson

ByRef not passing address
 
Ian,

ByRef is indeed the default method of passing parameters, but when you
enclose the parameter in parentheses in the call to the procedure, you are
forcing it to be passed ByVal.

Change
increment(myValue)
to
increment myValue


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com

"Ian Stanborough" wrote in message
...
Hope some one can help

My problem is when I try to create a function that will increment an
integer, the calling parameter I believe is passed as a value and not a
reference. The help file suggest that default value of parameters passed

is
ByRef, but this example tell me in the case of integer its not.

Does any one have a work around.

My email address is

All suggests are welcomed. A coded example would explain all


Program
dim myValue as integer
.....
myValue = 0
increment(myValue)
' here myValue still has value of 0, because it was not passed to
subroutine as reference and hence not incremented
....
end Program



sub increment( number as integer)
number = number + 1
end sub





Tom Ogilvy

ByRef not passing address
 
Ian,
Your causing the problem yourself by incorrectly calling the function.

increment(myValue)

shoud be

increment myValue

You only use parentheses if you are returning a value (calling a function)
or you are using Call

Call Increment(myValue)

--
Regards,
Tom Ogilvy


"Ian Stanborough" wrote in message
...
Hope some one can help

My problem is when I try to create a function that will increment an
integer, the calling parameter I believe is passed as a value and not a
reference. The help file suggest that default value of parameters passed

is
ByRef, but this example tell me in the case of integer its not.

Does any one have a work around.

My email address is
All suggests are welcomed. A coded example would explain all


Program
dim myValue as integer
.....
myValue = 0
increment(myValue)
' here myValue still has value of 0, because it was not passed to
subroutine as reference and hence not incremented
....
end Program



sub increment( number as integer)
number = number + 1
end sub






All times are GMT +1. The time now is 04:48 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com