Array Size

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.

 

Print | posted @ Saturday, November 27, 2004 12:43 AM

Comments on this entry:

Gravatar # re: Array Size
by Bill at 11/28/2004 2:45 AM

I'm not disagreeing with you at all - but I really get giddy over Pivot ;-)
  
Gravatar # re: Array Size
by Darren at 11/28/2004 3:07 PM

Don't you just declare
<br>
<br>Dim Foo() as string
  
Gravatar # re: Array Size
by Scott Allen at 11/29/2004 8:02 AM

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 have been closed on this topic.
Scott Allen
Posts - 869
Comments - 4493
Stories - 14