Byref and ByVal

  • Thread starter Thread starter thomasp
  • Start date Start date
T

thomasp

Will someone tell my the difference between ByRef and ByVal when passing
values between subs?

Thanks,

Thomas
 
Hi,

ByRef allows you to send changes made to the variable back. Any
changes made to variable sent byval are lost.

Ken
-----------------
Will someone tell my the difference between ByRef and ByVal when passing
values between subs?

Thanks,

Thomas
 
Hi,

Will someone tell my the difference between ByRef and ByVal when passing
values between subs?

easiest with two little samples illustrating the difference:

main proc:

sub Test
dim intMainNumber as integer=10

Test_ByVal(10)
messagebox.show "intMainNumber after calling ByVal-sub: " & _
intMainNumber.tostring

Test_ByRef(10)
messagebox.show "intMainNumber after calling ByRef-sub: " & _
intMainNumber.tostring
end sub

sub Test_ByVal(byval intSomeNumber as integer)
intSomeNumber+=10
messagebox.show("ByVal: " & intSomeNumber.tostring)
end sub

sub Test_ByRef(byref intSomeNumber as integer)
intSomeNumber+=10
messagebox.show("ByRef: " & intSomeNumber.tostring)
end sub

ByVal will create a new Integer-variable that'll be used in the
Test_ByVal-sub. ByRef instead will be provided with a pointer to the
memory-location where intSomeNumber is stored, thus intSomeNumber will
change and remain changed even after returning to the caller.

Cheers,
Olaf
 
Coming from C++ I was surprised and confused,
when I discovered that objects passed ByVal hasn't been copies.

Module Module1
Class A
Public i As Integer = 0
End Class

Sub testWithInt(ByVal iIntVal As Integer, ByRef iIntRef As Integer)
iIntVal = 10 'cannot change val of I1
iIntRef = 10 'can change val of I2
End Sub

Sub testWithClass(ByVal cIntVal As A, ByRef cIntRef As A)
cIntVal.i = 20 'can change vals of A1
' I didn't expect this, as there
' you would expect to get a copy of the object
cIntRef.i = 20 'can change vals of A2
End Sub

Sub testWithClassNew(ByVal cIntVal As A, ByRef cIntRef As A)
Dim C1 As New A
Dim C2 As New A
C1.i = 30
C2.i = 30
cIntVal = C1 'cannot change ref of B1
cIntRef = C2 'can change ref of B2
End Sub

Sub Main()
Dim A1 As New A
Dim A2 As New A
Dim B1 As New A
Dim B2 As New A
Dim I1 As Integer = 0
Dim I2 As Integer = 0
testWithInt(I1, I2)
testWithClass(A1, A2)
testWithClassNew(B1, B2)
Console.WriteLine(I1) '0
Console.WriteLine(I2) '10
Console.WriteLine(A1.i) '20
Console.WriteLine(A2.i) '20
Console.WriteLine(B1.i) '0
Console.WriteLine(B2.i) '30
End Sub
End Module
 
Klaus Wassmann said:
Coming from C++ I was surprised and confused,
when I discovered that objects passed ByVal hasn't been copies.

Really???
Making a transient, deep copy of an object just to pass it into some
method or other would be /damnably/ expensive.

Beside, in C++ you pass the Pointers /to/ the Objects around, not the
Objects themselves. I would expect the situation your code
demonstrates to be exactly the same, in C++, C# and VB.Net.

Regards,
Phill W.
 
Ken Tucker said:
Hi,

ByRef allows you to send changes made to the variable back. Any
changes made to variable sent byval are lost.

Ken

Unless you are passing a Reference type ByVal, in which case all changes are
made (to any properties of that object). With ByVal, you cannot, however,
assign it to a new object and have it passed back
 
Back
Top