473,614 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
+ 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 /
misunderstandin g the ByVal?

Richard
Nov 20 '05 #1
16 970

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*******@hotm ail.com> wrote in message
news:c5******** ***@otis.netspa ce.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 /
misunderstandin g 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*******@hotm ail.com> wrote in message
news:c5******** ***@otis.netspa ce.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 /
misunderstandin g 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*******@hotm ail.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*******@hotm ail.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 misunderstandin g 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.ValueTyp e

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.ValueTyp e

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*******@hotm ail.com> wrote in message
news:c5******** ***@otis.netspa ce.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*******@hotm ail.com> wrote in message
news:c5******** ***@otis.netspa ce.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 /
misunderstandin g the ByVal?

Richard


Nov 20 '05 #8
Richard,
As the others have suggested, you are misunderstandin g 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.ValueTyp e

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.ValueTyp e

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*******@hotm ail.com> wrote in message
news:c5******** ***@otis.netspa ce.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*******@hotm ail.com> wrote in message
news:c5******** ***@otis.netspa ce.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 /
misunderstandin g 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
383
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 points to something and changes it. The default for simple data types is ByVal and for objects, it's ByRef. Am I correct so far? If so, I still don't have a clue as
35
2159
by: Maxim Yegorushkin | last post by:
The following code: #include <iostream> class base { private: virtual ~base() { std::cout << "virtual ~base()\n";
16
1782
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 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 /
14
2499
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 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...
11
8114
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 number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub FirstDouble(ByVal array As Integer()) Dim i As Integer
8
2209
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 Module1 Class A
8
3267
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 ------->>>>>>http://www.NewsDemon.com<<<<<<------
0
8130
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8627
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8279
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8433
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6088
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5540
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2568
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 we have to send another system
1
1747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1425
bsmnconsultancy
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.