Procedure that changes parameters

rvaldivia
rvaldivia used Ask the Experts™
on
Hi, I'm new in Visual Basic, I need to know how to declare a procedure that modifies the variable used as parametres, something like this:

a=0
b=1

MyProcedure(a,b)
a=a+1
b=b+1
End procedure
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You need to make sure the variables are declared as ByRef (default VB behavior, but I would specify)

private sub MyProcedure(byref a as integer,byref b as integer)

Commented:
You can pass parameters in two ways ByRef and ByVal.  If you pass ByRef, the variable will be changed.  If you pass ByVal, the function makes a copy of the variable so that the original is not affected.  Here is an example that shows how to pass byref and bycval:

Option Explicit

Private Sub Command1_Click()
    Dim x As Integer, y As Integer
   
    x = 1
    y = 1
   
    Call sIncByRef(x)
    Call sIncByval(y)
   
    MsgBox "x is " & CStr(x) & " and y is " & CStr(y)
End Sub

Private Sub sIncByRef(ByRef intNum)
    intNum = intNum + 1
End Sub
Private Sub sIncByval(ByVal intNum)
    intNum = intNum + 1
End Sub

Commented:
Not that ByRef is the default behaviour so that

Private Sub sIncByRef(ByRef intNum as integer)
   intNum = intNum + 1
End Sub

is the same as:

Private Sub sIncByRef(intNum as integer)
   intNum = intNum + 1
End Sub
 

Author

Commented:
Thanx,
It works

Commented:
Thanx is good, an A grade is better

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial