473,471 Members | 1,729 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

byval still allows changes to be made


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
16 1760

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

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
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
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
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
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
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
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
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
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
"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
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
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...
35
by: Maxim Yegorushkin | last post by:
The following code: #include <iostream> class base { private: virtual ~base() { std::cout << "virtual ~base()\n";
16
by: Richard | last post by:
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...
14
by: Robin Tucker | last post by:
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...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
8
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...
8
by: thomasp | last post by:
Will someone tell my the difference between ByRef and ByVal when passing values between subs? Thanks, Thomas -- Posted via NewsDemon.com - Premium Uncensored Newsgroup Service...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.