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 14 2483
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
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
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
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
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
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
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
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
Correction. Typos
Seeing the postings from Robin I cannot think that it is a proplem for him.
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
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 > >
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
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!
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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
|
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...
|
by: VJ |
last post by:
Does ByRef work when calling a Method in DLL from a Executable?
VJ
|
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,...
| |
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...
|
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...
|
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...
|
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
|
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...
|
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...
| |
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |