byval vs byref

B

Boni

Dear all,
I found out that I don' understand byVal/byRef in VB.
There is a simple example:
Why in the first test the result is 10,10 where in the second 0,20.
Thanks for your help.
Boni
Module Module1

Class A

Public B As Integer

End Class

Sub test(ByVal val_ As A, ByRef ref_ As A)

val_.B = 10

ref_.B = 10

End Sub

Sub test2(ByVal aVal As A, ByRef aRef As A)

Dim A1 As New A

Dim A2 As New A

A1.B = 20

A2.B = 20

aVal = A1

aRef = A2

End Sub



Sub Main()

Dim A1 As A

Dim A2 As A

A1 = New A

A2 = New A

test(A1, A2)

Console.WriteLine(A1.B)

Console.WriteLine(A2.B)

A1 = New A

A2 = New A

test2(A1, A2)

Console.WriteLine(A1.B)

Console.WriteLine(A2.B)

End Sub

End Module
 
R

Robin Tucker

Think about it like this:

When you pass by reference you can create a new item with "New" and that new
item will be visible back to the caller. When you pass by reference, you
can change the state of the item, but you cannot assign a new one and have
that visible back to the caller.
 
K

Ken Tucker [MVP]

Hi,

Imagine you have a form that need to be filled out. If you give
a copy of the form to someone to fill out the orginal doesnt change (byval).
If you give them the orginal to fill out they hand it back to you changed
(byref). Hope that helps.

Ken
---------------
Dear all,
I found out that I don' understand byVal/byRef in VB.
There is a simple example:
Why in the first test the result is 10,10 where in the second 0,20.
Thanks for your help.
Boni
Module Module1

Class A

Public B As Integer

End Class

Sub test(ByVal val_ As A, ByRef ref_ As A)

val_.B = 10

ref_.B = 10

End Sub

Sub test2(ByVal aVal As A, ByRef aRef As A)

Dim A1 As New A

Dim A2 As New A

A1.B = 20

A2.B = 20

aVal = A1

aRef = A2

End Sub



Sub Main()

Dim A1 As A

Dim A2 As A

A1 = New A

A2 = New A

test(A1, A2)

Console.WriteLine(A1.B)

Console.WriteLine(A2.B)

A1 = New A

A2 = New A

test2(A1, A2)

Console.WriteLine(A1.B)

Console.WriteLine(A2.B)

End Sub

End Module
 
B

Bob Powell [MVP]

When you pass by value you pass a copy of the original. The code that
recieves the value can sometimes change it but when the subroutine is ended
the value, including any changes is destroyed along with the other values
that were in the scope of the method.

When you pass by reference you pass the address of the original item of
data. Any changes made in the subroutine are made to the original item of
data via that reference and when the suboroutine ends it's only the
refeference and not the data that is destroyed.

As a design choice you would pass by value when you simply want to inform
the subroutine of some parameter or another and not care about what the
subroutine does with it. You would pass by reference if you want the
subroutine to do some computation that permanently alters the data.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
C

Chris

The easy way to think about it is that it is just the same as pointer to
your object or var.
 
J

JohnFol

Robin / Ken / Bob / Chris, sorry but I read this differently. In Boni's 2 examples, both have the 2nd argument byRef.

I was going down the line of Reference Types vs Value types.
If you have MSDN locally installed, the following gives an almost identical example

ms-help://MS.MSDNQTR.2005JAN.1033/vbls7/html/vblrfVBSpec6_1.htm


Failing that search under the "Visual Basic Language Specification" for
7.1 Value Types and Reference Types
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top