473,405 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

An odd array feature in vb.net

I have two independent arrays and mysteriously they occupy the same
space. Is this an error or a feature?

Dim tokens$() '(input list of msn's and
constants)
Dim values() As Object '(tokens with msn's replaced by
values)

temp="A = 2 * B"

'---parse the temp expression into tokens
tokens = Split(temp, " ")

'---setup a similar sized values array
ReDim values(UBound(tokens))
values=tokens

and under vb.net changing tokens(4) will now change values(4)!!.

I expected that making values = tokens would give me an array "copy by
element". In fact it made the two occupy the same array space. Is
this a bug or a language feature??

Nov 18 '08 #1
5 2036

"barcrofter" <jd*****@gmail.comwrote in message
news:5d**********************************@a3g2000p rm.googlegroups.com...
>I have two independent arrays and mysteriously they occupy the same
space. Is this an error or a feature?

Dim tokens$() '(input list of msn's and
constants)
Dim values() As Object '(tokens with msn's replaced by
values)

temp="A = 2 * B"

'---parse the temp expression into tokens
tokens = Split(temp, " ")

'---setup a similar sized values array
ReDim values(UBound(tokens))
values=tokens

and under vb.net changing tokens(4) will now change values(4)!!.

I expected that making values = tokens would give me an array "copy by
element". In fact it made the two occupy the same array space. Is
this a bug or a language feature??
First a couple of comments:

Since you are using Dot.Net I would suggest using the new syntax so:
dim Tokens() as string

Ensure that you have options strict and explicit set to on. This will cause
you a bit of hassle at first but will pay big dividends when debugging your
app. You will then have to declare the temp variable.

As for you question you are setting a reference to an array to another array
so they will both point to the same array. What you might want to do is use
one of the Copy methods to bring the items from one array to the other.

Also if you are not looking to export this array to unmanaged code I would
suggest looking into another collection type for example a List(of T). This
will allow you to manipulate the List without having to redim all the time.

LS
Nov 18 '08 #2
On 2008-11-18, barcrofter <jd*****@gmail.comwrote:
I have two independent arrays and mysteriously they occupy the same
space. Is this an error or a feature?

Dim tokens$() '(input list of msn's and
constants)
Dim values() As Object '(tokens with msn's replaced by
values)

temp="A = 2 * B"

'---parse the temp expression into tokens
tokens = Split(temp, " ")

'---setup a similar sized values array
ReDim values(UBound(tokens))
values=tokens

and under vb.net changing tokens(4) will now change values(4)!!.

I expected that making values = tokens would give me an array "copy by
element". In fact it made the two occupy the same array space. Is
this a bug or a language feature??
Arrays are reference types - so nothing is copied, except the reference. If
you want a copy, then you need to do something like this:

redim values(ubound(tokens))
tokens.copyto(value, 0)

Though, be aware that if the array contains reference types, then all that is
copied in each element is the reference... so changing properties on an
object in one array will be reflected in the other.

--
Tom Shelton
Nov 18 '08 #3
"barcrofter" <jd*****@gmail.comschrieb
ReDim values(UBound(tokens))
values=tokens

and under vb.net changing tokens(4) will now change values(4)!!.

I expected that making values = tokens would give me an array "copy by
element". In fact it made the two occupy the same array space. Is
this a bug or a language feature??

Lookup "reference types" in the documentation (in opposite to value types).
An array is a reference type. You've just copied a reference. 'values'
references the same array as 'tokens'. You can ommit the "Redim" I quoted
because it's overwritten in the next line "values=tokens".
Armin

Nov 18 '08 #4
This is by design. See :
http://msdn.microsoft.com/en-us/libr...hs(VS.80).aspx
(Value Types and Reference Types)

As "A reference type contains a pointer to another memory location that
holds the data" and arrays are reference type it means that values=tokens
make the "values" variable to point to the same memory location than
"tokens" (and this is true for all objects).

See :
http://msdn.microsoft.com/en-us/libr...rray.copy.aspx for how to
copy the array content...

--
Patrice

"barcrofter" <jd*****@gmail.coma écrit dans le message de groupe de
discussion :
5d**********************************...oglegroups.com...
I have two independent arrays and mysteriously they occupy the same
space. Is this an error or a feature?

Dim tokens$() '(input list of msn's and
constants)
Dim values() As Object '(tokens with msn's replaced by
values)

temp="A = 2 * B"

'---parse the temp expression into tokens
tokens = Split(temp, " ")

'---setup a similar sized values array
ReDim values(UBound(tokens))
values=tokens

and under vb.net changing tokens(4) will now change values(4)!!.

I expected that making values = tokens would give me an array "copy by
element". In fact it made the two occupy the same array space. Is
this a bug or a language feature??
Nov 18 '08 #5
Wow! Humbling.
Nov 19 '08 #6

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

Similar topics

1
by: Raptor | last post by:
Hi, I'm quite new to MySQL and quite impressed by its feature set. I've also been looking at Interbase and it has a feature that allows a multidimensional array to be stored in a single field. ...
13
by: HappyHippy | last post by:
Hi, I'm wondering what you think about this piece of code: #include<iostream> int main() { int size; std::cin >> size;
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
4
by: mflll | last post by:
I am looking into the different techniques of handling arrays of edit boxes in Java Script. The first program below works fine. However, are there better ways of doing this, where the person...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
3
by: herrin | last post by:
Hello, I'm trying to assign strings to array elements in javascript. It works in Firefox but in IE it throws "Unknown runtime error". sContent = '<p>this is a features test post.</p><p><a...
13
by: sathyashrayan | last post by:
Dear group, pls go through the following function definition: function at_show_aux(parent, child) { var p = document.getElementById(parent); var c = document.getElementById(child); var top ...
9
by: IamIan | last post by:
I'm using an array to store map features (name, lat, lon, caption, etc), from which the user can then select an individual feature. The problem is that when thousands of features are stored in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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
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...
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
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...

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.