An Array question

E

Edward

Hi everybody,
To me the following code shouldn't work but it does !
Imports system.String
Dim x As String="This,is,a,test"
Dim y(1) As String
y=x.Split(",")
TextBox1.text=y(3)
why "Y" which is an array of lenght 2 accepts index 3 which is larger than
its lenght?
Any thoughts?
 
L

Lloyd Sheen

Edward said:
Hi everybody,
To me the following code shouldn't work but it does !
Imports system.String
Dim x As String="This,is,a,test"
Dim y(1) As String
y=x.Split(",")
TextBox1.text=y(3)
why "Y" which is an array of lenght 2 accepts index 3 which is larger
than
its lenght?
Any thoughts?

The statement:
y=x.Split(",")

creates a new array and assigns it to y. The first assignment allows for
items (0) and (1). The reassignment (the split statement) just returns an
array the size of which depends on the source string and the split string.

LS
 
E

Edward

yes but I was wondering about the logic behind this kind of behavior , when
we define a fixed size for an array isn't it suppose to keep its fixed size ?
In fact we can assign a larger array to a smaller array and VB doesn't
complian and resizes the smaller array!
 
H

Herfried K. Wagner [MVP]

Edward said:
To me the following code shouldn't work but it does !
Imports system.String
Dim x As String="This,is,a,test"
Dim y(1) As String

=> 'Dim y() As String'.
y=x.Split(",")
TextBox1.text=y(3)
why "Y" which is an array of lenght 2 accepts index 3 which is larger
than
its lenght?

'Y' references an array of length 4 with indices 0, ..., 3 after the
assignment of 'Split''s return value. 'Y(3)' contains "test".
 
L

Lloyd Sheen

Edward said:
yes but I was wondering about the logic behind this kind of behavior ,
when
we define a fixed size for an array isn't it suppose to keep its fixed
size ?
In fact we can assign a larger array to a smaller array and VB doesn't
complian and resizes the smaller array!

It really is no different than any other variable assignment. At one moment
you have an array with capacity of 2 (0) and (1). When you use the x.split
you assign a new array. The old one is put in for garbage collection unless
there is another reference to it.

Same as if I assign a new string to a string varaible. I think you are
taking the first assignment as a "set in concrete" statement where it is
not. Until another assignment happens (which is the split) it has one array
and then after the assignment (split) you now have a new one.

LS
 
S

ShaneO

Edward said:
yes but I was wondering about the logic behind this kind of behavior , when
we define a fixed size for an array isn't it suppose to keep its fixed size ?
In fact we can assign a larger array to a smaller array and VB doesn't
complian and resizes the smaller array!

You need to understand that "Split" basically does a REDIM on your
variable, however, if you really must limit the size of the Array, try
the following -

Dim x As String = "This,is,a,test"
Dim y() As String = Split(x, ",", 2)

You'll find that "y" is now limited to just two elements. (0 and 1)

I hope this helps.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
P

Phill W.

Edward said:
I was wondering about the logic behind this kind of behavior , when
we define a fixed size for an array isn't it suppose to keep its fixed size ?
Nope.

In fact we can assign a larger array to a smaller array and VB doesn't
complain and resizes the smaller array!

It /used/ to be that the equivalent, VB "Proper" code ...

Dim y(1) As String
y = Split(x, ",")

.... wouldn't even /compile/ precisely because of this discontinuity.

In our Brave New World.Net, however, Split() creates a whole /new/ array
and dumps a reference to it back into the variable "y", junking whatever
may or may not have been there before; there's no relationship at all
between the previous array and the one that you're now assigning to it.

HTH,
Phill W.
 
C

Chris Dunaway

You need to understand that "Split" basically does a REDIM on your
variable, however, if you really must limit the size of the Array, try
the following -

This is not accurate. The Split function does not ReDim the existing
array. Arrays are reference types. As pointed out earlier, Split
creates a *new* array and then assigns a reference to it to variable
y, discarding the old reference. There is no ReDim involved, you can
use Reflector to verify that.

Chris
 
C

Cor Ligthert[MVP]

Chris,

I wished I could tell you in Dutch


But what is in a word, there is done again a Dimension of an Array. So to
use the sentence ReDim is in my idea not that wrong. That ReDim keyword
restores the values of an old array is in my idea not the right use of the
meaning of the word in English.

However who am I to state that.

:)

Cor
 
S

ShaneO

Chris said:
This is not accurate. The Split function does not ReDim the existing
array. Arrays are reference types. As pointed out earlier, Split
creates a *new* array and then assigns a reference to it to variable
y, discarding the old reference. There is no ReDim involved, you can
use Reflector to verify that.

Chris

Thank-you Chris, you are technically correct, however as I wrote in my
post it "basically does a REDIM". At the end of the day the original
variables reference is replaced with a reference to a new array created
by the Split function, but so what, for the sake of simplicity in
replying to the OP question, it "basically does a REDIM". :)


ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
E

Edward

I guess I was not thinking 100% OOP ,although I knew this concept from Java
still I was thinking in terms of VBA but I'm back to the track now.
 
C

Chris Dunaway

Chris,

I wished I could tell you in Dutch

But what is in a word, there is done again a Dimension of an Array. So to
use the sentence ReDim is in my idea not that wrong. That ReDim keyword
restores the values of an old array is in my idea not the right use of the
meaning of the word in English.

I understand what you are saying. I just wanted to make sure the OP
realized that calling Split creates an entirely new array and then
assigns that to the array variable. I wanted to make it clear that
the existing array is not reused in some manner.

Cheers,

Chris
 
C

Chris Dunaway

Thank-you Chris, you are technically correct, however as I wrote in my
post it "basically does a REDIM". At the end of the day the original
variables reference is replaced with a reference to a new array created
by the Split function, but so what, for the sake of simplicity in
replying to the OP question, it "basically does a REDIM". :)

Yes I understood your meaning and agree. I did a little investigating
with Reflector and ReDim without Preserve basically does create a new
array. As I told Cor, I wanted the OP to be clear that Split does not
somehow modify or use the existing array, but you are right about
ReDim.

Chris
 
A

andrews

you could also write y() as string,(normal action), as there are 4
strings you have also indexes from 0 to 3
 
G

Göran Andersson

Edward said:
I guess I was not thinking 100% OOP ,although I knew this concept from Java
still I was thinking in terms of VBA but I'm back to the track now.

As VB has retained it's syntax for arrays, but not how they work, it can
get a bit confusing. The code doesn't always look like it does what it does.

This alternate syntax for declaring an array reference corresponds a bit
more to what the code actually does:

Dim x As String()

"x" is the name of the variable, and "String()" is the type of the variable.
 

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