UBound confusion

  • Thread starter Thread starter JR
  • Start date Start date
J

JR

I may have posted a blank post accidentally. Sorry

I am coming over to VB.NET from C++/C# and am confused about the UBound
function.

Example code:

Dim i As Integer

Dim myarray(6) As Integer

i = UBound(myarray)

I would expect that myarray is of length 6 and that the array
subscripts run from 0 to 5. The doc. for UBound says:

Returns the highest available subscript for the indicated dimension of
an array.

It also says:

Since array subscripts start at 0, the length of a dimension is greater
by one than the highest available subscript for that dimension.

So...I would expect the value of i above to be 5 but instead it is 6.

What's up??

-jr
 
JR said:
I may have posted a blank post accidentally. Sorry

I am coming over to VB.NET from C++/C# and am confused about the UBound
function.

Example code:

Dim i As Integer

Dim myarray(6) As Integer

i = UBound(myarray)

I would expect that myarray is of length 6 and that the array
subscripts run from 0 to 5. The doc. for UBound says:

Returns the highest available subscript for the indicated dimension of
an array.

It also says:

Since array subscripts start at 0, the length of a dimension is greater
by one than the highest available subscript for that dimension.

So...I would expect the value of i above to be 5 but instead it is 6.

What's up??

-jr

UBound is a careover function from VB6. Use myarray.GetUpperBound(0)
instead.

myarray(6) makes objects for 0 ... 6. not "6" objects.

Chris
 
JR,

Probably for compatible reasons is decided that in VBNet an array where is
written

dim a(6) as string contains 7 items.

Therefore when I need 6 items I use
dim a(5) as string

For the rest see the message from Chris.

I never use ubound or words like that.

a.lenght - 1 gives very nice the last item.

Luckily is AFAIK this the only place.

Cor
 
JR said:
I may have posted a blank post accidentally. Sorry

I am coming over to VB.NET from C++/C# and am confused about the UBound
function.
Actually...


Example code:

Dim i As Integer

Dim myarray(6) As Integer

i = UBound(myarray)

I would expect that myarray is of length 6 and that the array
subscripts run from 0 to 5.

.... the confusion lies here. While a C(etc) array defined as [6] does
indeed have 6 elements, indexed 0 1 2 3 4 5, a VB.NET array defined (6)
in fact has *seven* elements, indexed 0 1 2 3 4 5 6.

The reasons for this are 'historical' :) VB6 allowed arrays to have any
range of subscripts, using the To keyword: eg Dim a(-3 To 7) As
Integer, but the base index whtn you didn't use To was always 1. Or 0.
Depending on stuff. Anyway, the key point is that in Basic when you
declare an array (6) then there is an element (6).

I won't argue here about the relative merits of the C and Basic
approaches - as a C programmer you will have to bear the differences in
mind.
 
JR said:
I am coming over to VB.NET from C++/C# and am confused about the UBound
function.

Example code:

Dim i As Integer

Dim myarray(6) As Integer

The line above creates an array with 7 elements (indices 0 through 6). 6 is
the upper bound ('UBound').
 
Back
Top