|
-
Mar 26th, 2006, 08:18 PM
#1
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:
Private Sub Command1_Click()
Dim a As Long
Dim CollectionTest As New Collection
Dim StringTest As String
CollectionTest.Add "Data1"
CollectionTest.Add "Data2"
' Loop before passing to test contents
For a = 1 To CollectionTest.Count
MsgBox CollectionTest(a)
Next
' Pass it as ByVal
TestByVal CollectionTest
' The newly added value can be seen here
For a = 1 To CollectionTest.Count
MsgBox CollectionTest(a)
Next
StringTest = " Not passed yet "
MsgBox StringTest
TestAnotherByVal StringTest
MsgBox StringTest
End Sub
Private Sub TestByVal(ByVal c As Collection)
c.Add "Data3 - Created by ByVal"
End Sub
Private Sub TestAnotherByVal(ByVal s As String)
s = s & " was passed ByVal "
End Sub
-
Mar 26th, 2006, 08:20 PM
#2
Re: Confused with ByVal
Collection is an object and objects are always passed ByRef ...
-
Mar 26th, 2006, 08:21 PM
#3
Re: Confused with ByVal
Ahhh... Didn't know about that, thanks!
-
Mar 27th, 2006, 01:51 AM
#4
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:
Option Explicit
Private Sub Command1_Click()
Dim c As Collection
Set c = New Collection
c.Add "a"
c.Add "b"
Call test1(c)
MsgBox c.Count
End Sub
Private Sub Command2_Click()
Dim c As Collection
Set c = New Collection
c.Add "a"
c.Add "b"
Call test2(c)
MsgBox c.Count
End Sub
Private Sub test1(ByVal c As Collection)
Set c = New Collection
c.Add "c"
End Sub
Private Sub test2(ByRef c As Collection)
Set c = New Collection
c.Add "c"
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.
-
Mar 27th, 2006, 02:19 AM
#5
Conquistador
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?
-
Mar 27th, 2006, 02:31 AM
#6
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|