473,839 Members | 1,394 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can someone explain ByRef and ByVal for me please!?

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 here, where a reference is a pointer
(kind-of), a pointer is a pointer and passing an object by value dumps a
copy of the thing on the stack. I don't think things work like this in VB.
ByVal is not the same as C++ passing by value.

Can someone clear up my confusion and tell me where (apart from in COM
wrappers) I really need to use byref?

Thanks
Robin
Nov 20 '05 #1
14 2522
Hi,

When you pass a variable byref you can make changes to it and
pass it back to caller of the subroutine. When you pass it byval you cant
make changes to it.

Ken
----------------------
"Robin Tucker" <id************ *************@r eallyidont.com> wrote in
message news:cc******** ***********@new s.demon.co.uk.. .
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 here, where a reference is a pointer
(kind-of), a pointer is a pointer and passing an object by value dumps a
copy of the thing on the stack. I don't think things work like this in
VB.
ByVal is not the same as C++ passing by value.

Can someone clear up my confusion and tell me where (apart from in COM
wrappers) I really need to use byref?

Thanks
Robin

Nov 20 '05 #2
May I have a go please.

With a reference type, the value of a given object is, in fact, a reference.
Therefore passing it ByVal or ByRef is, essentially the same thing.

With a value type, passing it ByRef involves a reference to a given object
whereas passing it ByVal, as you say, 'dumps a copy of the thing on the
stack'.

The key to understanding ByRef and ByVal is in knowing wherther one is
dealing with a reference type object or a value type object.
"Robin Tucker" <id************ *************@r eallyidont.com> wrote in
message news:cc******** ***********@new s.demon.co.uk.. .
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 here, where a reference is a pointer
(kind-of), a pointer is a pointer and passing an object by value dumps a
copy of the thing on the stack. I don't think things work like this in VB. ByVal is not the same as C++ passing by value.

Can someone clear up my confusion and tell me where (apart from in COM
wrappers) I really need to use byref?

Thanks
Robin

Nov 20 '05 #3
Hi Robin,

This discussion thread shows it really nice in my opinon.

http://tinyurl.com/32buy

And read them all, although there is a (little) stupidity from me in it,
that is even interesting.

Cor
Nov 20 '05 #4
Ahhhhhhhhhhhhhh hhhhhhhhhhhhhhh hhh.
essentially no difference between byval and byref with reference types
(obviously). Value types passed by reference (integers for example) behave
as expected. Now I get it.

thanks.

"Stephany Young" <noone@localhos t> wrote in message
news:u5******** ******@TK2MSFTN GP12.phx.gbl...
May I have a go please.

With a reference type, the value of a given object is, in fact, a reference. Therefore passing it ByVal or ByRef is, essentially the same thing.

With a value type, passing it ByRef involves a reference to a given object
whereas passing it ByVal, as you say, 'dumps a copy of the thing on the
stack'.

The key to understanding ByRef and ByVal is in knowing wherther one is
dealing with a reference type object or a value type object.
"Robin Tucker" <id************ *************@r eallyidont.com> wrote in
message news:cc******** ***********@new s.demon.co.uk.. .
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 here, where a reference is a pointer
(kind-of), a pointer is a pointer and passing an object by value dumps a
copy of the thing on the stack. I don't think things work like this in

VB.
ByVal is not the same as C++ passing by value.

Can someone clear up my confusion and tell me where (apart from in COM
wrappers) I really need to use byref?

Thanks
Robin


Nov 20 '05 #5
Ok theres something thats been confusing me too. Pass by value and cannot
return a new object created in the method you are calling and referenced by
that variable. Pass by reference and the reference to the new object
persists after the call.

Its starting to make sense but I would have thought the designers of VB
would have made things much more explicit. imho it seems a little confusing
for a new coder.

"Cor Ligthert" <no**********@p lanet.nl> wrote in message
news:uJ******** ******@TK2MSFTN GP11.phx.gbl...
Hi Robin,

This discussion thread shows it really nice in my opinon.

http://tinyurl.com/32buy

And read them all, although there is a (little) stupidity from me in it,
that is even interesting.

Cor

Nov 20 '05 #6

It seems you are still slightly confused... The easiest way I can put it
would be.....

This is our main sub...

sub main()
dim strTest as string
strTest = "testing"
myMethod(strTes t)
msgbox(strTest)
end sub

---- pretty simple...

example with byVal
--------------------------------
sub myMethod(byVal strData as string)
strData = "lalala"
msgbox(strData)
end sub

example with byRef
--------------------------------
sub myMethod(byRef strData as string)
strData = "lalala"
msgbox(strData)
end sub

<snip>

When you use byVal, you will get the following result --> Msgbox
"lalala", then Msgbox "testing"

When you use byRef, you will get Msgbox "lalala", Msgbox "lalala"

The reason been is when you use byVAL, you send the VALue, ie. you send
a copy of the data in which for your method to use...

When you use byREF, you send a REFERENCE in which where to find the
data, so when you modify the data in your method, you are actually
modifying the same data in your calling method (in this case, sub main)

Those coming from a c (or similar) background would be fimilar with this
concept.. the reasoning for it is another long story....

Anyway, test the code for yourself, hopefully I havent confused you :P

Les Hughes


Robin Tucker wrote:
Ok theres something thats been confusing me too. Pass by value and cannot
return a new object created in the method you are calling and referenced by
that variable. Pass by reference and the reference to the new object
persists after the call.

Its starting to make sense but I would have thought the designers of VB
would have made things much more explicit. imho it seems a little confusing
for a new coder.

"Cor Ligthert" <no**********@p lanet.nl> wrote in message
news:uJ******** ******@TK2MSFTN GP11.phx.gbl...
Hi Robin,

This discussion thread shows it really nice in my opinon.

http://tinyurl.com/32buy

And read them all, although there is a (little) stupidity from me in it,
that is even interesting.

Cor


Nov 20 '05 #7
No, I understood that. But the issue is that this isn't the case for
reference types. If I pass in a reference type by value and modify it then
that modification remains after I have exited the method. The only time
this isn't the case is if I change the item referred to in the method, ie:

the output will be:

"By Value"
"Testing"

"By Reference"
"By Reference"
....

Dim myObject as SomeObject = new SomeObject()

myObject.Title = "Testing"

PassByValue(myO bject)
MessageBox(myOb ject.Title)

PassByReference (myObject)
MessageBox(myOb ject.Title)

....

public sub PassByValue ( byVal theObject as SomeObject)

theObject = new SomeObject()
theObject.Title = "By Value"

MessageBox(theO bject.Title)

end sub

public sub PassByReference ( byRef theObject as SomeObject)

theObject = new SomeObject()
theObject.Title = "By Reference"

MessageBox(theO bject.Title)
end sub
"Les Hughes" <le************ *@datarev.com.a u> wrote in message
news:Od******** ******@TK2MSFTN GP12.phx.gbl...

It seems you are still slightly confused... The easiest way I can put it
would be.....

This is our main sub...

sub main()
dim strTest as string
strTest = "testing"
myMethod(strTes t)
msgbox(strTest)
end sub

---- pretty simple...

example with byVal
--------------------------------
sub myMethod(byVal strData as string)
strData = "lalala"
msgbox(strData)
end sub

example with byRef
--------------------------------
sub myMethod(byRef strData as string)
strData = "lalala"
msgbox(strData)
end sub

<snip>

When you use byVal, you will get the following result --> Msgbox
"lalala", then Msgbox "testing"

When you use byRef, you will get Msgbox "lalala", Msgbox "lalala"

The reason been is when you use byVAL, you send the VALue, ie. you send
a copy of the data in which for your method to use...

When you use byREF, you send a REFERENCE in which where to find the
data, so when you modify the data in your method, you are actually
modifying the same data in your calling method (in this case, sub main)

Those coming from a c (or similar) background would be fimilar with this
concept.. the reasoning for it is another long story....

Anyway, test the code for yourself, hopefully I havent confused you :P

Les Hughes


Robin Tucker wrote:
Ok theres something thats been confusing me too. Pass by value and cannot return a new object created in the method you are calling and referenced by that variable. Pass by reference and the reference to the new object
persists after the call.

Its starting to make sense but I would have thought the designers of VB
would have made things much more explicit. imho it seems a little confusing for a new coder.

"Cor Ligthert" <no**********@p lanet.nl> wrote in message
news:uJ******** ******@TK2MSFTN GP11.phx.gbl...
Hi Robin,

This discussion thread shows it really nice in my opinon.

http://tinyurl.com/32buy

And read them all, although there is a (little) stupidity from me in it,
that is even interesting.

Cor



Nov 20 '05 #8
Hi Les,

I think that only for few people the passing of a value which you show gives
any problems. Seeing the postings from Robin I think that it is a proplem
for him. The problem can be when we start thinking about an object.

With an object means Byval actualy the passing of the value of an adres from
the object.
However when it does not exist it is not possible to pass the value of that
adres.

Than you can pass that byref, the value of the object is filled when you
create it and than given back in the reference from the function you call.

It is not that difficult, however can be confusing.

Cor

Nov 20 '05 #9
Correction. Typos

Seeing the postings from Robin I cannot think that it is a proplem for him.
Nov 20 '05 #10

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
5511
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
4
12329
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)
6
1699
by: VJ | last post by:
Does ByRef work when calling a Method in DLL from a Executable? VJ
4
2476
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
1898
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
10276
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()
8
2217
by: Boni | last post by:
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
6
1957
by: ari | last post by:
hey all, i have the following 2 classes: Public Class DataAccessLayer .... .... Public Sub GetRecords(ByRef ds As DataSet1) ds = New DataSet1
0
9856
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
9698
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10597
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
10656
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
10299
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
5684
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...
1
4495
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
4071
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3136
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.