Results 1 to 5 of 5

Thread: How to differentiate ByVal and ByRef [RESOLVED]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    http://www.vbforums.com
    Posts
    164

    Thumbs up How to differentiate ByVal and ByRef [RESOLVED]

    Hi all,

    I'm a bit confused by these by value and by reference. Can anyone show me an example of using this two? I can't differentiate them.

    For example, is the following call byref or byval?

    VB Code:
    1. Private sub Form_Load
    2. dim blnCorrect as boolean
    3.  
    4.     call TrueOrFalse(blnCorrect)
    5.     if blnCorrect = True then
    6.            msgbox "True"
    7.     else
    8.            msgbox "False"
    9.     end if
    10. End Sub
    11.  
    12. Private sub Passing(a as boolean)
    13.     .....
    14.     .....
    15.     a = Not a
    16. End Sub

    Please advice.

    Regards,
    Jeremy
    Last edited by jeremy_ckw; Apr 22nd, 2003 at 03:36 AM.
    "If ignorance is bliss, that probably explain why I'm in a such a mess right now!!"

  2. #2
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    ByRef will pass the pointer and ByRef will pass the actual contents
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim strTemp As String, strReturn As String
    3. strTemp = "A variable"
    4. strReturn = usingByVal(strTemp)
    5. MsgBox "Orginal Variable: " & strTemp & vbCrLf & vbCrLf & _
    6.         "Returned Result: " & strReturn
    7.  
    8. strReturn = usingByRef(strTemp)
    9. MsgBox "Orginal Variable: " & strTemp & vbCrLf & vbCrLf & _
    10.         "Returned Result: " & strReturn
    11.  
    12.  
    13. strReturn = usingDefault(strTemp)
    14. MsgBox "Orginal Variable: " & strTemp & vbCrLf & vbCrLf & _
    15.         "Returned Result: " & strReturn
    16.  
    17. 'Using byRef the following function
    18. strTemp = "Some Text"
    19. strTemp = usingByRef(strTemp)
    20. MsgBox strTemp
    21. 'Can be written as
    22. strTemp = "Some Text"
    23. usingByRef strTemp
    24. MsgBox strTemp
    25. End Sub
    26.  
    27. Private Function usingByVal(ByVal uVal As String) As String
    28.     'the contents of the variable is passed, the variable remains unchanged"
    29.     uVal = uVal & ". This variable has changed"
    30.     usingByVal = uVal
    31. End Function
    32.  
    33. Private Function usingByRef(ByRef uVal As String) As String
    34.     'the pointer for the variable is passed. therefore
    35.     'the original variable also changes
    36.     uVal = uVal & ". This variable has changed"
    37.     usingByRef = uVal
    38. End Function
    39.  
    40. Private Function usingDefault(uVal As String) As String
    41.     'Work out what this is!
    42.     uVal = uVal & ". This variable has changed"
    43.     usingDefault = uVal
    44. End Function
    HTH

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  3. #3
    Fanatic Member daydee's Avatar
    Join Date
    Jun 2001
    Location
    Canada
    Posts
    560
    ByRef (by reference)
    A way of passing the address of an argument to a procedure instead of passing the value. This allows the procedure to access the actual variable. As a result, the variable's actual value can be changed by the procedure to which it is passed. Unless otherwise specified, arguments are passed by reference.

    ByVal (by value)
    A way of passing the value of an argument to a procedure instead of passing the address. This allows the procedure to access a copy of the variable. As a result, the variable's actual value can't be changed by the procedure to which it is passed.

    From your example... if TrueOrFalse was a sub like so:
    VB Code:
    1. Public Sub TrueOrFalse(ByRef blnCorrect As Boolean)
    2. Dim intCount As Integer
    3.  
    4. intCount = 2 + 2
    5.  
    6. If intCount = 4 Then
    7.     blnCorrect = True
    8. Else
    9.     blnCorrect = False
    10. End If
    11.  
    12. End Sub
    then the blnCorrect variable in your Form_Load event would return MsgBox "True" message.

    Better examples will probably come along here so hang on and checkem out too.

    Hope this helps!
    Give your music collection a whole new life with PartyTime Jukebox

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    http://www.vbforums.com
    Posts
    164

    Thanks!


    VB Code:
    1. Private Function usingDefault(uVal As String) As String
    2.     'Work out what this is!
    3.     uVal = uVal & ". This variable has changed"
    4.     usingDefault = uVal
    5. End Function
    If I'm not mistaken then the default should be by reference since it changed the original variable value. Thanks for making sure I understand!

    Thanks..both of you!

    Regards,
    Jeremy
    "If ignorance is bliss, that probably explain why I'm in a such a mess right now!!"

  5. #5
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Ur Welcome. Cheers

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

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