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.

 

posted on Friday, November 26, 2004 4:43 PM by scott

Comments

Saturday, November 27, 2004 6:45 PM by Bill

# re: Array Size

I'm not disagreeing with you at all - but I really get giddy over Pivot ;-)
Sunday, November 28, 2004 7:07 AM by Darren

# re: Array Size

Don't you just declare

Dim Foo() as string
Monday, November 29, 2004 12:02 AM by Scott Allen

# re: Array Size

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 "tricky".