By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,262 Members | 2,664 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,262 IT Pros & Developers. It's quick & easy.

Can someone explain ByRef and ByVal for me please!?

P: n/a
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
Share this Question
Share on Google+
14 Replies


P: n/a
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*************************@reallyidont.com> wrote in
message news:cc*******************@news.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

P: n/a
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*************************@reallyidont.com> wrote in
message news:cc*******************@news.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

P: n/a
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

P: n/a
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.

"Stephany Young" <noone@localhost> wrote in message
news:u5**************@TK2MSFTNGP12.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*************************@reallyidont.com> wrote in
message news:cc*******************@news.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

P: n/a
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**********@planet.nl> wrote in message
news:uJ**************@TK2MSFTNGP11.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

P: n/a

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


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**********@planet.nl> wrote in message
news:uJ**************@TK2MSFTNGP11.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

P: n/a
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
"Les Hughes" <le*************@datarev.com.au> wrote in message
news:Od**************@TK2MSFTNGP12.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(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


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**********@planet.nl> wrote in message
news:uJ**************@TK2MSFTNGP11.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

P: n/a
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

P: n/a
Correction. Typos

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

P: n/a

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

Robin Tucker wrote:
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
"Les Hughes" <le*************@datarev.com.au> wrote in message
news:Od**************@TK2MSFTNGP12.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(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


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**********@planet.nl> wrote in message
news:uJ**************@TK2MSFTNGP11.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 #11

P: n/a
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.

"Les Hughes" wrote:

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

Robin Tucker wrote:
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
"Les Hughes" <le*************@datarev.com.au> wrote in message
news:Od**************@TK2MSFTNGP12.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(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


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**********@planet.nl> wrote in message
news:uJ**************@TK2MSFTNGP11.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 21 '05 #12

P: n/a
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

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:91**********************************@microsof t.com...
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.

"Les Hughes" wrote:

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

Robin Tucker wrote:
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
"Les Hughes" <le*************@datarev.com.au> wrote in message
news:Od**************@TK2MSFTNGP12.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(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
>
>
>
>
>
>
>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**********@planet.nl> wrote in message
>>news:uJ**************@TK2MSFTNGP11.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
>>>
>>>
>>
>>
>>

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.732 / Virus Database: 486 - Release Date: 7/29/2004
Nov 21 '05 #13

P: n/a
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!
Nov 21 '05 #14

P: n/a
JBK
Carl and Rory of DotNetRocks fame have a free video explaining just this,
its well worth watching:

http://www.franklins.net/video/objec...ObjectIsA.html

Bryan

"Hal Rosser" <hm******@bellsouth.net> wrote in message
news:OR*************@bignews4.bellsouth.net...
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

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:91**********************************@microsof t.com...
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.

"Les Hughes" wrote:

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

Robin Tucker wrote:

> 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
>
>
> "Les Hughes" <le*************@datarev.com.au> wrote in message
> news:Od**************@TK2MSFTNGP12.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(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
>>
>>
>>
>>
>>
>>
>>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**********@planet.nl> wrote in message
>>>news:uJ**************@TK2MSFTNGP11.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
>>>>
>>>>
>>>
>>>
>>>
>
>
>

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.732 / Virus Database: 486 - Release Date: 7/29/2004

Nov 21 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.