Results 1 to 11 of 11

Thread: [RESOLVED] ByVal

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Resolved [RESOLVED] ByVal

    I have seen ByVal used so many times, but what is it and whats the purpose of it?

  2. #2

  3. #3
    Addicted Member
    Join Date
    Jun 2006
    Posts
    172

    Re: ByVal

    Taken from: http://www.developerfusion.co.uk/show/51/6/

    No need to reinvent the wheel.

    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
    VB Code:
    1. Private Sub TestSub(ByVal strPath As String)
    2.  
    3. Passing ByRef
    4. Private Sub TestSub(ByRef strPath As String) ' or just
    5. 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 variable any other way.

    The following example shows the differences:

    VB Code:
    1. Sub Form_Load()
    2.     Dim strTest As String
    3.     '// fill the variable
    4.     strTest = "Hello from Form_Load"
    5.     '// call the procedure
    6.     Call TestSub(strTest)
    7.     '// display a message box containing the value of strTest
    8.     Msgbox strTest
    9. End Sub
    10.  
    11. '// TestSub procedure when passing ByVal
    12. Sub TestSub(ByVal strString As String)
    13.     strString = "Hello from TestSub"
    14.     '// when control returns to Form_Load, no changes will have
    15.     '// been made to strTest
    16. End Sub
    17.  
    18. '// TestSub procedure when passing ByRef
    19. Sub TestSub(ByRef strString As String)
    20.     strString = "Hello from TestSub"
    21.     '// when control returns to Form_Load, the value of
    22.     '// strTest will have changed to Hello from TestSub
    23. 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:

    VB Code:
    1. Private Function TestFunction(ByRef sString As String)
    2.     sString = sString & vbCrLf
    3. End Function
    4.  
    5. Private Sub Command1_Click()
    6.     Dim sTestString
    7.     TestFunction sTestString
    8. End Sub

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

    VB Code:
    1. Private Function TestFunction(ByRef sString As String)
    2.     sString = sString & vbCrLf
    3. End Function
    4.  
    5. Private Sub Command2_Click()
    6.     Dim sTestString As String
    7.     TestFunction sTestString
    8. End Sub
    • If you found my post to be helpful, please rate me.

  4. #4

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: ByVal

    Quote Originally Posted by BrailleSchool
    So the value of ByVal cannot be modified but ByRef can be modified?
    Exactly. As was mentioned in MWagner's post, ByVal just passes the value, whereas ByRef passes a reference (an address in memory) so that the variable can be changed.

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: ByVal

    ByVal is just the value. If I tell you I have $5.28 in my pocket, you can't change it. If I give you the $5.28 to count for yourself, you can change it.

    Since a sub has the ByRef variable itself, not just its value, it can change it. You're giving it the actual variable (by giving it the address of that variable), not just telling it what the variable is worth.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: ByVal

    Note that using ByRef makes it easy to pass back two or more values from a Function or even a Sub. Here is a trivial example. You could add more ByRef parameters if needed.

    VB Code:
    1. Public Function xxx(ByRef x As Integer) As Boolean
    2.     x = x + 1
    3.     xxx = True
    4. End Function

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: ByVal

    Quote Originally Posted by MartinLiss
    Note that using ByRef makes it easy to pass back two or more values from a Function or even a Sub. Here is a trivial example. You could add more ByRef parameters if needed.

    VB Code:
    1. Public Function xxx(ByRef x As Integer) As Boolean
    2.     x = x + 1
    3.     xxx = True
    4. End Function
    If I read that right, xxx is a boolean flag and x is a number that being incremented by 1. x can be changed because its a ByRef. So if it was ByVal x As Integer, x = x + 1 would then bring an error?

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: ByVal

    also, if i understood this thread correctly,
    VB Code:
    1. Public Function xxx(ByRef x As Integer) As Boolean
    2.     x = x + 1
    3.     xxx = True
    4. End Function
    would be the same as
    VB Code:
    1. Public Function xxx(x As Integer) As Boolean
    2.     x = x + 1
    3.     xxx = True
    4. End Function

  10. #10

  11. #11

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width