Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

byval still allows changes to be made

13 views
Skip to first unread message

Richard

unread,
Apr 9, 2004, 11:24:32 PM4/9/04
to

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


Richard

unread,
Apr 9, 2004, 11:32:03 PM4/9/04
to

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" <raka...@hotmail.com> wrote in message
news:c57pdf$17s6$1...@otis.netspace.net.au...

Cor Ligthert

unread,
Apr 10, 2004, 2:22:28 AM4/10/04
to
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

Tom Shelton

unread,
Apr 10, 2004, 2:25:11 AM4/10/04
to
On 2004-04-10, Richard <raka...@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.

Jay B. Harlow [MVP - Outlook]

unread,
Apr 10, 2004, 11:13:19 AM4/10/04
to
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" <raka...@hotmail.com> wrote in message

news:c57pri$1801$1...@otis.netspace.net.au...

Cor Ligthert

unread,
Apr 10, 2004, 11:38:12 AM4/10/04
to
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


Armin Zingler

unread,
Apr 10, 2004, 5:58:34 AM4/10/04
to
"Richard" <raka...@hotmail.com> schrieb

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

Jay B. Harlow [MVP - Outlook]

unread,
Apr 10, 2004, 4:38:42 PM4/10/04
to
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" <notfir...@planet.nl> wrote in message
news:erGfRJx...@tk2msftngp13.phx.gbl...

Cor Ligthert

unread,
Apr 11, 2004, 3:37:37 AM4/11/04
to
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


0 new messages