Can someone explain ByRef and ByVal for me please!?

R

Robin Tucker

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
 
K

Ken Tucker [MVP]

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
 
S

Stephany Young

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.
 
C

Cor Ligthert

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
 
R

Robin Tucker

Ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh.
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.
 
R

Robin Tucker

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.
 
L

Les Hughes

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(strTest)
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
 
C

Cor Ligthert

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
 
R

Robin Tucker

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(myObject)
MessageBox(myObject.Title)

PassByReference(myObject)
MessageBox(myObject.Title)

....

public sub PassByValue ( byVal theObject as SomeObject)

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

MessageBox(theObject.Title)

end sub

public sub PassByReference( byRef theObject as SomeObject)

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

MessageBox(theObject.Title)
end sub
 
C

Cor Ligthert

Correction. Typos

Seeing the postings from Robin I cannot think that it is a proplem for him.
 
L

Les Hughes

Now I understand,

With refrence types, they will always be passed as refrence types within
objects, this is a PITB issue, but done for a reason....

A way around this, is to 'clone' the object....

This can be done by introducing iClonable..

ie
Public Class theObject
Implements ICloneable

then implementing
Public Function Clone() As Object Implements System.ICloneable.Clone '
only provides a shallow clone
Return Me.MemberwiseClone
End Function

As commented, this implements a 'shallow' clone. To read more about
cloning, just google for it, you will find plenty of articles that
discuss deep and shallow cloning, and the difference, and the howto's.

Here is some other misc code I found...

' example of making a clone of an object called "person"
'Public Function Clone() As Person
' Dim BinFormatter As New Binary.BinaryFormatter
' Dim memStream As New System.IO.MemoryStream
' BinFormatter.Serialize(memStream, Me)
' memStream.Position = 0
' Return CType(BinFormatter.Deserialize _
' (memStream), Person1)
'End Function

Found it a while ago, and kept it in a project in case I need it in the
future :p

Hope this has helped,

Les Hughes
 
G

Guest

That works great for classes that have the "serializable" attribute set.
However, I have been unable to get this to work with structures. I try to
set the structure attribute to "serializable" but it doesn't work, at least
using the syntax I was using.
 
H

Hal Rosser

passing values ByRef means you are passing a reference to the variable (its
location in memory)
passing ByVal means you are passing a copy of 'Value of' the variable.
if you pass ByRef - you can change the original variable from the 'Calling'
procedure.
If you pass ByVal - you don't change the original. - a copy of it becomes a
local variable in the called procedure.
Passing ByRef is a way to get multiple Return values from a procedure
I don't know what all the serializable hubub is about - just explaining the
Question in the Subject line
 
C

Codo

To understand ByRef and ByVal properly, you must understand what is the
stack and the heap.

the stack is an area that holds native values and is very quick. Integers
and doubles are held in the stack. "pointers" are held in the stack as
well. The heap is a larger area of memory, and you usually store objects
in the heap. But to access the heap, you have always a pointer in the
stack. That why, when you pass an object ByVal, what you're doing is
passing the "pointer" ByVal (in the stack). ByVal and ByRef applies only
to things in the stack. And a pointer in the stack may point to something
in the heap.

And by the way, the difference between classes and structures is that
instances of classes are help in the heap with a pointer in the stack,
while a structure is entirely held in the stack.

Hope this helps!
 

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