473,770 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

byval vs byref

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.WriteLi ne(A1.B)

Console.WriteLi ne(A2.B)

A1 = New A

A2 = New A

test2(A1, A2)

Console.WriteLi ne(A1.B)

Console.WriteLi ne(A2.B)

End Sub

End Module
Nov 21 '05 #1
8 2214
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.

"Boni" <oilia@nospam > wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
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.WriteLi ne(A1.B)

Console.WriteLi ne(A2.B)

A1 = New A

A2 = New A

test2(A1, A2)

Console.WriteLi ne(A1.B)

Console.WriteLi ne(A2.B)

End Sub

End Module

Nov 21 '05 #2
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
---------------
"Boni" <oilia@nospam > wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
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.WriteLi ne(A1.B)

Console.WriteLi ne(A2.B)

A1 = New A

A2 = New A

test2(A1, A2)

Console.WriteLi ne(A1.B)

Console.WriteLi ne(A2.B)

End Sub

End Module

Nov 21 '05 #3
Well that was a load of bollocks (edited below!):

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 value, you
can change the state of the item, but you cannot assign a new one and have
that visible back to the caller.

Nov 21 '05 #4
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.

"Boni" <oilia@nospam > wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
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.WriteLi ne(A1.B)

Console.WriteLi ne(A2.B)

A1 = New A

A2 = New A

test2(A1, A2)

Console.WriteLi ne(A1.B)

Console.WriteLi ne(A2.B)

End Sub

End Module

Nov 21 '05 #5
The easy way to think about it is that it is just the same as pointer to
your object or var.
"Boni" <oilia@nospam > wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
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.WriteLi ne(A1.B)

Console.WriteLi ne(A2.B)

A1 = New A

A2 = New A

test2(A1, A2)

Console.WriteLi ne(A1.B)

Console.WriteLi ne(A2.B)

End Sub

End Module

Nov 21 '05 #6
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.2005 JAN.1033/vbls7/html/vblrfVBSpec6_1. htm
Failing that search under the "Visual Basic Language Specification" for
7.1 Value Types and Reference Types
"Boni" <oilia@nospam > wrote in message news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
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.WriteLi ne(A1.B)

Console.WriteLi ne(A2.B)

A1 = New A

A2 = New A

test2(A1, A2)

Console.WriteLi ne(A1.B)

Console.WriteLi ne(A2.B)

End Sub

End Module

Nov 21 '05 #7
byref is a point to an object you have already created
byval is the object it self

"Boni" <oilia@nospam > wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
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.WriteLi ne(A1.B)

Console.WriteLi ne(A2.B)

A1 = New A

A2 = New A

test2(A1, A2)

Console.WriteLi ne(A1.B)

Console.WriteLi ne(A2.B)

End Sub

End Module

Nov 21 '05 #8
JP
If you pass a variable ByVal, the procedure cannot modify the variable
itself. But, if the argument is a reference type, you can modify the members
of the object to which it points, even though you cannot replace the object
itself.

If you pass a variable ByRef, the procedure can modify the variable itself.
If the argument is an object variable, you can assign a new object to it as
well.

Like in

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim a As Integer, b As Integer
a = 10
b = 20
ValRef(a, b)
MsgBox("a=" & a & " ; b=" & b) 'Return a=10 ; b=25
ValRefTextBox(T extBox1, TextBox2) 'Set TextBox1.Text=1 0 and
TextBox2.Text=2 0
End Sub

Private Sub ValRef(ByVal a, ByRef b)
a += 5
b += 5
End Sub

Private Sub ValRefTextBox(B yVal t1 As TextBox, ByRef t2 As TextBox)
t1.Text = 10
t2.Text = 20
End Sub

Cheers,
JP
------------------------------------------------------------------
A program is a device used to convert,
data into error messages
------------------------------------------------------------------
"Chris" <cc*********@ho tmail.com> wrote in message
news:um******** ********@TK2MSF TNGP12.phx.gbl. ..
byref is a point to an object you have already created
byval is the object it self

"Boni" <oilia@nospam > wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
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.WriteLi ne(A1.B)

Console.WriteLi ne(A2.B)

A1 = New A

A2 = New A

test2(A1, A2)

Console.WriteLi ne(A1.B)

Console.WriteLi ne(A2.B)

End Sub

End Module


Nov 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
383
by: Sandy | last post by:
Hello! Help!!!! I have ten zillion books that attempt to describe the difference between ByVal and ByRef and none of them are clear to me. I have gathered that ByVal makes a copy and ByRef points to something and changes it. The default for simple data types is ByVal and for objects, it's ByRef. Am I correct so far? If so, I still don't have a clue as
7
5509
by: Hei | last post by:
Hi, i know the difference of ByRef and ByVal, in case if use byref or byval don't affect the result which one should prefer? (less memory use, better performance ....issue) thx
19
2419
by: Rob Panosh | last post by:
Hello, Ok here is the senerio: ..... Dim myArrayList as New ArrayList(0) me.Test_A( myArrayList )
4
12327
by: Carlos Gomez | last post by:
In VB6 the default for passing variables was ByRef. It was faster and used less memory. Why did MS changed that? Are there any advantages using ByVal over ByRef? (other than ByVal impeding you from changing the original variable passed)
14
2516
by: Robin Tucker | last post by:
Although I've been working on this project for 8 months now, I'm still not sure of the difference between ByVal and ByRef. As most objects in VB are reference types, passing ByVal I've discovered allows me to store a reference to the object I passed by val, to change that object and for the change to be reflected in the callers copy of the reference (confused?). Well, what is byref for in that case? I'm coming from a C++ background...
14
1538
by: Niklas | last post by:
Hi What I have learned is that a variable is just a reference when dealing with Objects. Are you supposed to use ByVal or ByRef in functions? They produce the same result or have I missed something? Regards /Niklas Public Class Main Shared Sub Main() Dim testPropObj As New MyPropertObject testPropObj.MyInt = 1
4
2473
by: Warren Sirota | last post by:
Hi, Please let me know if I am interpreting this correctly. I've done a little testing of the difference between passing parameters byVal and byRef, and the results were slightly non-intuitive, as I expected. I haven't seen this behavior explained precisely in the .net world yet, so I wanted to check and make sure I've got it right. I apologize that this is a bit long. I've tried to keep it concise. There are code fragments here, but...
7
1893
by: barrett bonden | last post by:
Is there any way to pass parameters to a function and simply know there will get there without the silly (C like ) complexity of worring about byval and or perhaps byref ? (Why bother to have two languages in the first place now ....silliness ; OOP taken to dysfunctional absurdity ! This idea is too counter productive to last; MS , your king has no clothes on ...)
2
10273
by: Witold Iwaniec via .NET 247 | last post by:
It seems that when you pass an object to a function it is always passed by reference even if it is explicitly declared ByVal. Is it the behavior of VB.Net? Here is sample code from sample Asp.Net application. The sub loadValueByVal takes the argument by value so after returning to calling method, the object should be unchanged but it is not Public Class ITest Private MyName As String Public TestId As String Public Sub New()
0
9602
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8905
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5326
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3987
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.