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

byval still allows changes to be made

P: n/a

Hi,

I am passing a structure to a subroutine where the passed parameter has been
declared as ByVal.

However, changes made to the passed variable inside the subroutine flow
through to the actual variable that has been passed over, even though with
ByVal this should not happen.

Has anybody else discovered this or am I doing completely wrong /
misunderstanding the ByVal?

Richard
Nov 20 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a

My apologies, it is a CLASS not a structure that I am passing over. Still -
VB .NET should not change it if it has been passed over as ByVal, true?

Richard

"Richard" <ra*******@hotmail.com> wrote in message
news:c5***********@otis.netspace.net.au...

Hi,

I am passing a structure to a subroutine where the passed parameter has been declared as ByVal.

However, changes made to the passed variable inside the subroutine flow
through to the actual variable that has been passed over, even though with
ByVal this should not happen.

Has anybody else discovered this or am I doing completely wrong /
misunderstanding the ByVal?

Richard

Nov 20 '05 #2

P: n/a

My apologies, it is a CLASS not a structure that I am passing over. Still -
VB .NET should not change it if it has been passed over as ByVal, true?

Richard

"Richard" <ra*******@hotmail.com> wrote in message
news:c5***********@otis.netspace.net.au...

Hi,

I am passing a structure to a subroutine where the passed parameter has been declared as ByVal.

However, changes made to the passed variable inside the subroutine flow
through to the actual variable that has been passed over, even though with
ByVal this should not happen.

Has anybody else discovered this or am I doing completely wrong /
misunderstanding the ByVal?

Richard

Nov 20 '05 #3

P: n/a
Hi Richard,

In VB.net byval passes the value from the reference from an object and the
value from a value.

I hope this helps?

Cor

Nov 20 '05 #4

P: n/a
Hi Richard,

In VB.net byval passes the value from the reference from an object and the
value from a value.

I hope this helps?

Cor

Nov 20 '05 #5

P: n/a
On 2004-04-10, Richard <ra*******@hotmail.com> wrote:

My apologies, it is a CLASS not a structure that I am passing over. Still -
VB .NET should not change it if it has been passed over as ByVal, true?


Actually, it should. A class is a reference type. When a reference
type is passed ByVal, it is the reference to the object that is passed
by value - not the object. That means you can't change the reference to
point to a different object (well you can locally, but the change does
not propagate back to the caller) - but since the reference points to
the actual object on the heap, then changes to the objects state are
propagated back to the caller.

--
Tom Shelton [MVP]
Powered By Gentoo Linux 1.4
There are two ways to write error-free programs; only the third one works.
Nov 20 '05 #6

P: n/a
On 2004-04-10, Richard <ra*******@hotmail.com> wrote:

My apologies, it is a CLASS not a structure that I am passing over. Still -
VB .NET should not change it if it has been passed over as ByVal, true?


Actually, it should. A class is a reference type. When a reference
type is passed ByVal, it is the reference to the object that is passed
by value - not the object. That means you can't change the reference to
point to a different object (well you can locally, but the change does
not propagate back to the caller) - but since the reference points to
the actual object on the heap, then changes to the objects state are
propagated back to the caller.

--
Tom Shelton [MVP]
Powered By Gentoo Linux 1.4
There are two ways to write error-free programs; only the third one works.
Nov 20 '05 #7

P: n/a
Richard,
As the others have suggested, you are misunderstanding Value & Reference
Parameters when used with Value & Reference Types!

A Class is a Reference Type, only a single instance of an object exists on
the heap, when pass a Class variable ByVal a copy of the reference is made,
hence the variable & parameter both refer to the same object, allowing your
routine to make changes to the object itself.

Remember there are two types of Parameters (ByRef & ByVal) and there are two
types of variables (Reference Types & Value Types).

So you can have:
ByRef - Reference Type
ByRef - Value Type
ByVal - Reference Type
ByVal - Value Type

Lets look at types of variables:
When you define a Class you are defining a Reference Type. Which means that
a variable of this Class holds a reference to the object, the object itself
exists on the heap. If I assign this variable to a second variable a copy of
this reference is made and I now have two references to the same object on
the heap. There is still only one object on the heap. If you define a
Structure you are defining a Value Type. The variable itself holds the value
of the structure. If I assign this variable to a second variable a copy of
the entire structure is made. I now have two copies of the same structure.

Value Types include:
Boolean, Byte, Short, Integer, Long, Char, Single, Double, Decimal, along
with anything defined with the Structure or Enum keyword.
Value Types all derive directly or indirectly from System.ValueType

Reference Types include:
Object, and anything defined with the Class, Interface or Delegate keyword
are reference types.
Reference Types all derive from System.Object excluding types that inherit
from System.ValueType

Interface is a reference type, even if defined in a Structure. The structure
itself is a value type, however if you assign the structure to a Interface
variable, it will be Boxed, boxing places the value on the heap in a new
object (effectively making it a reference type)

Lets look at types of parameters:
Now when you define a parameter to be ByVal a copy of the variable is
passed. Remember Reference types hold a reference to the object, so passing
a Reference Type ByVal causes a copy of this reference to be passed as a
parameter, the single copy of the object itself is still on the heap. The
variable & parameter both have references to this single object. Passing a
Value Type ByVal causes a complete copy of the value to be passed as a
parameter. Now passing a Reference Type ByRef, causes a reference to the
variable to be passed, the variable has a reference to the object. Passing a
Value Type ByRef also causes a reference to the variable to be passed, the
variable has a copy of the Value.

Remember ByVal & ByRef are how parameters are passed. Reference & Value
Types are how quantities are stored.

Although the following is in C# the concepts apply equally to VB.NET:
http://www.yoda.arachsys.com/csharp/parameters.html
Hope this helps
Jay
"Richard" <ra*******@hotmail.com> wrote in message
news:c5***********@otis.netspace.net.au...

My apologies, it is a CLASS not a structure that I am passing over. Still - VB .NET should not change it if it has been passed over as ByVal, true?

Richard

"Richard" <ra*******@hotmail.com> wrote in message
news:c5***********@otis.netspace.net.au...

Hi,

I am passing a structure to a subroutine where the passed parameter has

been
declared as ByVal.

However, changes made to the passed variable inside the subroutine flow
through to the actual variable that has been passed over, even though with ByVal this should not happen.

Has anybody else discovered this or am I doing completely wrong /
misunderstanding the ByVal?

Richard


Nov 20 '05 #8

P: n/a
Richard,
As the others have suggested, you are misunderstanding Value & Reference
Parameters when used with Value & Reference Types!

A Class is a Reference Type, only a single instance of an object exists on
the heap, when pass a Class variable ByVal a copy of the reference is made,
hence the variable & parameter both refer to the same object, allowing your
routine to make changes to the object itself.

Remember there are two types of Parameters (ByRef & ByVal) and there are two
types of variables (Reference Types & Value Types).

So you can have:
ByRef - Reference Type
ByRef - Value Type
ByVal - Reference Type
ByVal - Value Type

Lets look at types of variables:
When you define a Class you are defining a Reference Type. Which means that
a variable of this Class holds a reference to the object, the object itself
exists on the heap. If I assign this variable to a second variable a copy of
this reference is made and I now have two references to the same object on
the heap. There is still only one object on the heap. If you define a
Structure you are defining a Value Type. The variable itself holds the value
of the structure. If I assign this variable to a second variable a copy of
the entire structure is made. I now have two copies of the same structure.

Value Types include:
Boolean, Byte, Short, Integer, Long, Char, Single, Double, Decimal, along
with anything defined with the Structure or Enum keyword.
Value Types all derive directly or indirectly from System.ValueType

Reference Types include:
Object, and anything defined with the Class, Interface or Delegate keyword
are reference types.
Reference Types all derive from System.Object excluding types that inherit
from System.ValueType

Interface is a reference type, even if defined in a Structure. The structure
itself is a value type, however if you assign the structure to a Interface
variable, it will be Boxed, boxing places the value on the heap in a new
object (effectively making it a reference type)

Lets look at types of parameters:
Now when you define a parameter to be ByVal a copy of the variable is
passed. Remember Reference types hold a reference to the object, so passing
a Reference Type ByVal causes a copy of this reference to be passed as a
parameter, the single copy of the object itself is still on the heap. The
variable & parameter both have references to this single object. Passing a
Value Type ByVal causes a complete copy of the value to be passed as a
parameter. Now passing a Reference Type ByRef, causes a reference to the
variable to be passed, the variable has a reference to the object. Passing a
Value Type ByRef also causes a reference to the variable to be passed, the
variable has a copy of the Value.

Remember ByVal & ByRef are how parameters are passed. Reference & Value
Types are how quantities are stored.

Although the following is in C# the concepts apply equally to VB.NET:
http://www.yoda.arachsys.com/csharp/parameters.html
Hope this helps
Jay
"Richard" <ra*******@hotmail.com> wrote in message
news:c5***********@otis.netspace.net.au...

My apologies, it is a CLASS not a structure that I am passing over. Still - VB .NET should not change it if it has been passed over as ByVal, true?

Richard

"Richard" <ra*******@hotmail.com> wrote in message
news:c5***********@otis.netspace.net.au...

Hi,

I am passing a structure to a subroutine where the passed parameter has

been
declared as ByVal.

However, changes made to the passed variable inside the subroutine flow
through to the actual variable that has been passed over, even though with ByVal this should not happen.

Has anybody else discovered this or am I doing completely wrong /
misunderstanding the ByVal?

Richard


Nov 20 '05 #9

P: n/a
Hi Jay,

I had this week a long discussion with Jon in the dotnet general group. You
know I like arguing with Jon and I did give him a change in something which
has not real my intrest. (He is probably as serious as you however with you
I never should play, with Jon it is something as angling, but do not think I
do not respect him, I do for sure).

I have seen you writing about this before and at the end I told to Jon you
did do.
---------------------
Now passing a Reference Type ByRef, causes a reference to the
variable to be passed, the variable has a reference to the object. Passing a
Value Type ByRef also causes a reference to the variable to be passed, the
variable has a copy of the Value.
----------------------
And I said to him passing a ref is in a way passing a boxed reference (Now I
see this I am in doubt).

I also can not come clear with a NT and the heap, maybe you have a link for
me that I can read something about that. Why I am in doubt you think maybe,
that is because I cannot connect that with the 360 but more with a Unix
system. (And when I am sure, than I can maybe real arguing with guys like
you and Jon about this).

Cor
Nov 20 '05 #10

P: n/a
Hi Jay,

I had this week a long discussion with Jon in the dotnet general group. You
know I like arguing with Jon and I did give him a change in something which
has not real my intrest. (He is probably as serious as you however with you
I never should play, with Jon it is something as angling, but do not think I
do not respect him, I do for sure).

I have seen you writing about this before and at the end I told to Jon you
did do.
---------------------
Now passing a Reference Type ByRef, causes a reference to the
variable to be passed, the variable has a reference to the object. Passing a
Value Type ByRef also causes a reference to the variable to be passed, the
variable has a copy of the Value.
----------------------
And I said to him passing a ref is in a way passing a boxed reference (Now I
see this I am in doubt).

I also can not come clear with a NT and the heap, maybe you have a link for
me that I can read something about that. Why I am in doubt you think maybe,
that is because I cannot connect that with the 360 but more with a Unix
system. (And when I am sure, than I can maybe real arguing with guys like
you and Jon about this).

Cor
Nov 20 '05 #11

P: n/a
"Richard" <ra*******@hotmail.com> schrieb

I am passing a structure to a subroutine where the passed parameter
has been declared as ByVal.

However, changes made to the passed variable inside the subroutine
flow through to the actual variable that has been passed over, even
though with ByVal this should not happen.

Has anybody else discovered this or am I doing completely wrong /
misunderstanding the ByVal?


1. ByVal passes a copy of the variable content.
2. ByRef passes a reference to the variable.

3. Is the variable type a value type, the variable contains the object.
4. Is the variable type a reference type, the variable contains the
reference to the object. The reference is the pointer (= an address,
currently 4 bytes) to the memory location containing the object.

That's actually all you need to know.

Consequently:

=> Passing a value type byval copies the object. Changes in the called
procedure affect the copy, not the original.

=> Passing a reference type byval copies the reference, so that there are
two references to the same object. Changing the reference in the called
procedure affect the copy, not the original. Changing the object changes the
object that's reference has been passed.

=> Passing a value type ByRef passes a reference to the variable. This is
also a reference to the object. Changes to the object are changes to the
original object because no copy has been made. Changes to the reference do
not affect the original object, i.e. the local argument gets a new reference
to a different object.

=> Passing a reference type ByRef passes a reference to the variable. As the
variable contains the reference, the passed reference is a reference to the
reference to the object. Changes to the object affect the original object.
Changes to the reference are changes to the passed variable.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #12

P: n/a
"Richard" <ra*******@hotmail.com> schrieb

I am passing a structure to a subroutine where the passed parameter
has been declared as ByVal.

However, changes made to the passed variable inside the subroutine
flow through to the actual variable that has been passed over, even
though with ByVal this should not happen.

Has anybody else discovered this or am I doing completely wrong /
misunderstanding the ByVal?


1. ByVal passes a copy of the variable content.
2. ByRef passes a reference to the variable.

3. Is the variable type a value type, the variable contains the object.
4. Is the variable type a reference type, the variable contains the
reference to the object. The reference is the pointer (= an address,
currently 4 bytes) to the memory location containing the object.

That's actually all you need to know.

Consequently:

=> Passing a value type byval copies the object. Changes in the called
procedure affect the copy, not the original.

=> Passing a reference type byval copies the reference, so that there are
two references to the same object. Changing the reference in the called
procedure affect the copy, not the original. Changing the object changes the
object that's reference has been passed.

=> Passing a value type ByRef passes a reference to the variable. This is
also a reference to the object. Changes to the object are changes to the
original object because no copy has been made. Changes to the reference do
not affect the original object, i.e. the local argument gets a new reference
to a different object.

=> Passing a reference type ByRef passes a reference to the variable. As the
variable contains the reference, the passed reference is a reference to the
reference to the object. Changes to the object affect the original object.
Changes to the reference are changes to the passed variable.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #13

P: n/a
Cor,
And I said to him passing a ref is in a way passing a boxed reference (Now I see this I am in doubt). Neither the "Common Language Infrastructure Annotated Standard" by James S.
Miller from Addision Wesley, nor the "Common Language Infrastructure
Standard" itself uses the term "boxed reference" that I see.

And I said to him passing a ref is in a way passing a boxed reference (Now I see this I am in doubt). I'm not sure what you are actually trying to say here?

If you have a doubt I would recommend reading the CLI Standard. You can
purchase the Annotated Standard as I have, its worth having as it is the CLI
Standard with annotations (explainations), or you can read the CLI Standard
online in the "\Program Files\Microsoft Visual Studio .NET
2003\SDK\v1.1\Tool Developers Guide" folder for VS.NET 2003.

I find it helps to have had compiler theory training when you read the CLI
standard.

Hope this helps
Jay

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:er*************@tk2msftngp13.phx.gbl... Hi Jay,

I had this week a long discussion with Jon in the dotnet general group. You know I like arguing with Jon and I did give him a change in something which has not real my intrest. (He is probably as serious as you however with you I never should play, with Jon it is something as angling, but do not think I do not respect him, I do for sure).

I have seen you writing about this before and at the end I told to Jon you
did do.
---------------------
Now passing a Reference Type ByRef, causes a reference to the
variable to be passed, the variable has a reference to the object. Passing a Value Type ByRef also causes a reference to the variable to be passed, the
variable has a copy of the Value.
----------------------
And I said to him passing a ref is in a way passing a boxed reference (Now I see this I am in doubt).

I also can not come clear with a NT and the heap, maybe you have a link for me that I can read something about that. Why I am in doubt you think maybe, that is because I cannot connect that with the 360 but more with a Unix
system. (And when I am sure, than I can maybe real arguing with guys like
you and Jon about this).

Cor

Nov 20 '05 #14

P: n/a
Cor,
And I said to him passing a ref is in a way passing a boxed reference (Now I see this I am in doubt). Neither the "Common Language Infrastructure Annotated Standard" by James S.
Miller from Addision Wesley, nor the "Common Language Infrastructure
Standard" itself uses the term "boxed reference" that I see.

And I said to him passing a ref is in a way passing a boxed reference (Now I see this I am in doubt). I'm not sure what you are actually trying to say here?

If you have a doubt I would recommend reading the CLI Standard. You can
purchase the Annotated Standard as I have, its worth having as it is the CLI
Standard with annotations (explainations), or you can read the CLI Standard
online in the "\Program Files\Microsoft Visual Studio .NET
2003\SDK\v1.1\Tool Developers Guide" folder for VS.NET 2003.

I find it helps to have had compiler theory training when you read the CLI
standard.

Hope this helps
Jay

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:er*************@tk2msftngp13.phx.gbl... Hi Jay,

I had this week a long discussion with Jon in the dotnet general group. You know I like arguing with Jon and I did give him a change in something which has not real my intrest. (He is probably as serious as you however with you I never should play, with Jon it is something as angling, but do not think I do not respect him, I do for sure).

I have seen you writing about this before and at the end I told to Jon you
did do.
---------------------
Now passing a Reference Type ByRef, causes a reference to the
variable to be passed, the variable has a reference to the object. Passing a Value Type ByRef also causes a reference to the variable to be passed, the
variable has a copy of the Value.
----------------------
And I said to him passing a ref is in a way passing a boxed reference (Now I see this I am in doubt).

I also can not come clear with a NT and the heap, maybe you have a link for me that I can read something about that. Why I am in doubt you think maybe, that is because I cannot connect that with the 360 but more with a Unix
system. (And when I am sure, than I can maybe real arguing with guys like
you and Jon about this).

Cor

Nov 20 '05 #15

P: n/a
Hi Jay,

Thank you, I will have a look to it and although I never wanted, maybe I
will spent some time to find information and look at the architecture of NT.

However did you take a look to that little piece of sample I did make for
Charles, I think I did succeed in the challenge

:-)

Cor
Nov 20 '05 #16

P: n/a
Hi Jay,

Thank you, I will have a look to it and although I never wanted, maybe I
will spent some time to find information and look at the architecture of NT.

However did you take a look to that little piece of sample I did make for
Charles, I think I did succeed in the challenge

:-)

Cor
Nov 20 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.