↑ Grab this Headline Animator

Monday, May 19, 2008

Difference between ByRef and ByVal

When getting the value of a variable from a procedure or function parameter you have two ways to request it. Either you can request for it to be passed ByRef (default), or ByVal.
Passing ByValPrivate Sub TestSub(ByVal strPath As String)
Passing ByRefPrivate Sub TestSub(ByRef strPath As String) ' or justPrivate Sub TestSub(strPath As String)
When you pass ByVal, Visual Basic passes a copy of the variable to the procedure. This way the procedure gets the value, but any changes it makes will not affect the original variable.
When you pass ByRef, Visual Basic passes a pointer to the procedure. This is a reference so that the procedure knows where to find the variable in the memory. Any changes the procedure makes to this variable will effect the original one, as they are the same thing, however the variable does not need to be declared as public if you were wanting the procedure to access the variable any other way.

Submit this story to DotNetKicks

No comments:

Post a Comment

Post your comments/questions/feedback for this Article.


Latest Articles