An odd array feature in vb.net

B

barcrofter

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??
 
A

Armin Zingler

barcrofter said:
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
 
L

Lloyd Sheen

barcrofter said:
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
 
T

Tom Shelton

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top