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

An odd array feature in vb.net

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a

"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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
Wow! Humbling.
Nov 19 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.