Pulkit Gaur

dream - do - deserve

Visual Basic ByRef gotcha

Filed under: Visual Basic at 11:57:45 On 2006-06-14

A few dayz back my friend raghav was working on one ActiveX for company project.

He got stuck at one point where his procedure was changing the value of a variable while using ByRef.( The project is done in Visual Basic)

I also tried to figure out the problem but in vain frown.

BTW i never use ByRef in ActiveX control's coz i prefer to use classes most of the time.

Later ,searching this on net i found that whenever you enclose the passed variable in parenthesis Visual basic evaluates this as an expression and your passed variable becomes a ByVal passed variable.

So,

//Code Begin

foo(myInt)

..............................

...............................

Private Sub foo(myVar)

myVar = newValue

End Sub

//Code End

Will not change the value of myInt to newValue.

Rather :

//Code Begin

foo myInt

..............................

...............................

Private Sub foo(myVar)

myVar = newValue

End Sub

//Code End

Will do the Trick

Strange but true cool

PS: Anywayz Microsoft has made ByVal as a default method to pass variables in dotNET.

1 Comment(s) | Add your own


Leave a Comment

Required

Required, hidden