Array Size

Saturday, November 27, 2004
During my occasional forays into the land of Dim, Sub, and IsNothing, I’ve needed to remind myself that the number in an array definition represents the upper bound.

Dim months(11) As String 'an array with upper bound 11 has 12 elements

It feels weird, but as long as I kept telling myself the number represented the upper bound, and not the length of the array, everything seemed OK.

Then one day I needed an array of 0 elements.

Dim foo(-1) As String 'an array of 0 elements is declared with 0 – 1 = -1. 

I can’t get this statement out of my head. The illusion is broken. I now think of the number not as the upper bound, but as the length of the array minus 1.

Suddenly, writing array code in VB requires more thought and attention to detail with all of these subtractions going on.

 


Comments
Bill Sunday, November 28, 2004
I'm not disagreeing with you at all - but I really get giddy over Pivot ;-)
Darren Sunday, November 28, 2004
Don't you just declare
<br>
<br>Dim Foo() as string
Scott Allen Monday, November 29, 2004
Well, you can if someone else is actually going to create the array for you. But if you need to specify the size, that's when it gets &quot;tricky&quot;.
Comments are now closed.
by K. Scott Allen K.Scott Allen
My Pluralsight Courses
The Podcast!