Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 37,677 times

Contents

Related Categories

Variables - ByRef and ByVal

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 ByVal
Private Sub TestSub(ByVal strPath As String)

Passing ByRef
Private Sub TestSub(ByRef strPath As String) ' or just
Private 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 variabl any other way. The following example shows the differences:

Sub Form_Load()
    Dim strTest As String
    '// fill the variable
    strTest = "Hello from Form_Load"
    '// call the procedure
    Call TestSub(strTest)
    '// display a message box containing the value of strTest
    Msgbox strTest
End Sub

'// TestSub procedure when passing ByVal
Sub TestSub(ByVal strString As String)
    strString = "Hello from TestSub"
    '// when control returns to Form_Load, no changes will have
    '// been made to strTest
End Sub

'// TestSub procedure when passing ByRef
Sub TestSub(ByRef strString As String)
    strString = "Hello from TestSub"
    '// when control returns to Form_Load, the value of
    '// strTest will have changed to Hello from TestSub
End Sub

Note that when you are passing a variable ByRef, it must be declared as a specific datatype (ie string). Otherwise, VB cannot pass a pointer to it. If you do not do this, you will get a compile error:

ByRef Argument Type Mismatch.

This does not occur when passing ByVal. For example, the code below will produce a compile error when you press Command1:

Private Function TestFunction(ByRef sString As String)
    sString = sString & vbCrLf
End Function

Private Sub Command1_Click()
    Dim sTestString
    TestFunction sTestString
End Sub

while, the code below would not, as you have explicitly declared sTestString As String.

Private Function TestFunction(ByRef sString As String)
    sString = sString & vbCrLf
End Function

Private Sub Command2_Click()
    Dim sTestString As String
    TestFunction sTestString
End Sub

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • new solution

    Posted by jawsper on 17 Apr 2004

    i just found out how i can make my original idea work.

    i read something about the "eval" function in the microsotf script controls, so now ik kan just say: ScriptControl1.Eval(line) and it will fin...

  • Posted by LouisRose on 02 Mar 2004

    Hey cwamsley,

    Welcome to DevFusion!

    This sounds like a control array problem - if you're using VB (and not .NET) that is. You can have several controls with the same name, but with an index, lik...

  • String to Object Continued

    Posted by cwamsley on 01 Mar 2004

    Louis,

    I am working on a somewhat similar problem (to the String to Object comments from Aug 5, 2003) and hope you can suggest a course of action.

    I'm trying to create a simple way to set and mo...

  • Posted by Rde on 01 Mar 2004

    The following lists the public, private, and default visibility
    for the various elements you can declare in your programs.

    Default means declaring without anything proceding (or with Dim
    in the c...

  • Posted by LouisRose on 05 Aug 2003

    I'm glad that you came up with a solution.

    For muti-lingual support you best bet is to use a Resource file. Then the User's language and Locale will be detected automatically and your program will ...