Results 1 to 6 of 6

Thread: Confused with ByVal

  1. #1

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Arrow Confused with ByVal

    I have the ff. test code, could anyone point out what's happening here? Why is the passed Collection acting like it was passed ByRef?

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim a               As Long
    3.     Dim CollectionTest  As New Collection
    4.     Dim StringTest      As String
    5.    
    6.     CollectionTest.Add "Data1"
    7.     CollectionTest.Add "Data2"
    8.    
    9.     ' Loop before passing to test contents
    10.     For a = 1 To CollectionTest.Count
    11.         MsgBox CollectionTest(a)
    12.     Next
    13.     ' Pass it as ByVal
    14.     TestByVal CollectionTest
    15.     ' The newly added value can be seen here
    16.     For a = 1 To CollectionTest.Count
    17.         MsgBox CollectionTest(a)
    18.     Next
    19.    
    20.     StringTest = " Not passed yet "
    21.     MsgBox StringTest
    22.     TestAnotherByVal StringTest
    23.     MsgBox StringTest
    24. End Sub
    25.  
    26. Private Sub TestByVal(ByVal c As Collection)
    27.     c.Add "Data3 - Created by ByVal"
    28. End Sub
    29.  
    30. Private Sub TestAnotherByVal(ByVal s As String)
    31.     s = s & " was passed ByVal "
    32. End Sub
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  2. #2

  3. #3

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Confused with ByVal

    Ahhh... Didn't know about that, thanks!
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: Confused with ByVal

    It is not so simple.
    Objects act differently when passed ByVal.
    The object is not duplicated or cloned, instead a reference to the object is passed ByVal.
    I know this sounds confusing; I will try to explain.

    When an object is passed ByVal, a copy of the reference is passed.
    This means that the same object now has 2 references to it. The reference counter will be 2.
    You can change the object with either reference, but you can't change the original reference.

    When an object is passed ByRef, no extra reference is created.
    In fact a reference to the object variable is passed.
    The function can not only change the object, but also the reference.

    Here is an example to demonstrate this:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4. Dim c As Collection
    5.     Set c = New Collection
    6.     c.Add "a"
    7.     c.Add "b"
    8.     Call test1(c)
    9.     MsgBox c.Count
    10. End Sub
    11.  
    12. Private Sub Command2_Click()
    13. Dim c As Collection
    14.     Set c = New Collection
    15.     c.Add "a"
    16.     c.Add "b"
    17.     Call test2(c)
    18.     MsgBox c.Count
    19. End Sub
    20.  
    21. Private Sub test1(ByVal c As Collection)
    22.     Set c = New Collection
    23.     c.Add "c"
    24. End Sub
    25.  
    26. Private Sub test2(ByRef c As Collection)
    27.     Set c = New Collection
    28.     c.Add "c"
    29. End Sub

    So in short:
    When an object is passed ByRef, a reference to the object variable is passed.
    When an object is passed ByVal, a copy of the object variable is passed.

    In both ways, the original object can be modified inside the function, but the original object variable can only be changed if it is passed ByRef.
    Frans

  5. #5
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    Re: Confused with ByVal

    Is this similar to pointers in c?

    As in it will update the object that is passed, rather than a copy?

  6. #6
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: Confused with ByVal

    Yes, with ByVal, not a copy of the object is created, but a copy of the object variable.
    Both variables have a reference to the same object, so any changes to the object will be persistent. A reference is in fact the same as a pointer.
    With ByVal two variables have a pointer to the same object, with ByRef one variable is a pointer to the other variable, instead of a pointer to the object. The objects reference counter will still be 1.

    Otherwise ByVal would be an easy way to clone an object, but alas, that is more work.
    Frans

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