Accessing Element (0,0) in Zero-Bound Arrays

I never really liked to work with the zero part of zero bound arrays, but now that an array I am working with (declared using Dim x(100,100) As Object) stores data in x(0,0), x(0,1) etc, I need to read these elements.  

When looking inside an array at run-time using QuickWatch, there is a valid e.g. string value in the (0,0) element, that is, this element is not "Nothing" or Null, etc.  However, I can't access the (0,0) element using for example msgbox(x(0,0)) without throwing an exception that the index is wrong.  For some reason it needs x(1,1) --> and this shows the x(0,0) element value.  

The question is, if I have always been only reading the (1,1) element and higher in previous work to truly access the (1,1) element, why do I need to use (1,1) this time to see the contents of (0,0)?   Note, it is declared as Object, rather than Double, or Single, etc. so is this a reason?
Who is Participating?
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
I've had a quick look through that thread - it does appear to be this you have found yourself.
There doesn't seem to be a solution (in fact it looks like there is confusion).

I think this is actually a bug.  In .net the lower bound must be zero (dim x(1 to 100) will fail) but the excel array seems to be one based.  It is by default in VBA.

(I'm guessing here) The array is being taken across with a one base BUT the quick watch only understands zero base and so shows the values based on what it understands, not what is in reality.
Roopesh ReddyIT AnalystCommented:

Arrays always starts with 0th index. I think there is something wrong!

Can you show us the code?

Note, it is declared as Object, rather than Double, or Single, etc.

What do you mean by that?
David Johnson, CD, MVPOwnerCommented:
Perhaps this can turn on the lightbulb!

Zero-Based and One-Based Collections

A collection can be zero-based or one-based, depending on what its starting index is. The former means that the index of the first item in the collection is 0, and the latter means that it is 1. An example of a zero-based collection is the .NET Framework Controls collection, discussed earlier on this page. The Visual Basic Collection object is an example of a one-based collection.

One-based collections can be more intuitive to Visual Basic users, because the index ranges from 1 through the value of the Count Property (Collection Object), which returns the number of items in a collection. The index of a zero-based collection, by contrast, ranges from 0 through one less than the value of the Count property. This can be appropriate when the index values are offsets from a base value or correspond to members of a zero-based enumeration.

.NET Framework collections are zero-based for the purpose of standardization. The Visual Basic Collection class is one-based for the purpose of compatibility with previous versions.
Never miss a deadline with

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Roopesh ReddyIT AnalystCommented:

Object Array starts with 0th index -

Are you using Collections object???
As object is the type of data the array will hold. It is still a zero bound array.

Try this example, in access in a command click event

Dim x(100,100) As Object
Set x(0,0) = Me 'assigns the form object to the first element

'display object properties
Debug.Print x(0,0).Name  ' displays the form name
Debug.Print x(0,0)..WIndoeHeight 'displays the form width

Open in new window

Check your code if it is properly assigning values to array elements.
AndyAinscowFreelance programmer / ConsultantCommented:
This works perfectly here:
zero based dimension
lep1Author Commented:
Here is an example of the code that throws an exception when trying to access element (0,0) in the x(,) array.  It reads an Excel file that casts the range data into the x(,) array after dimensioning x(,) based on the range's row count and column count.  

To implement the code, add an OperFileDialog1 to your form, and paste the following code into e.g. a button.  You will need to ensure there are valid string or numerical values cells A1, A2, B1, B2 in the Excel that is opened. At run-time, for some reason, the element x(0,0) is unreadable, even though there will be data shown in the x(0,0) element when viewed with QuickWatch:

        Dim i, j As Integer
        Dim filename As String
        Dim x(,) As Object
        Dim myx(,) As Object
        With OpenFileDialog1
            .FileName = "*.*"
            .CheckFileExists = True
            .ShowReadOnly = True
            .Filter = "Excel 2010 *.xlxs|*.xlxs|Excel 2003-2007 *.xls|*.xls"
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                'Load file
                filename = .FileName
            End If
        End With

        ' Create new Application.
        Dim rXL As New Excel.Application
        Dim rWB As Excel.Workbook
        Dim rSheet As Excel.Worksheet
        Dim rRng As Excel.Range
        rWB = rXL.Workbooks.Open(filename)
        For i = 1 To 1 '  rWB.Sheets.Count To 1 Step -1
            Dim sheet As Excel.Worksheet = rWB.Sheets(i)
            Dim r As Excel.Range = sheet.UsedRange
            If r.Rows.Count = 0 Then Exit For
            Dim eCellArray As System.Array = r.Value
            ReDim x(r.Rows.Count, r.Columns.Count)
            x = r.Value
            rRng = Nothing
        ReDim myx(x.GetLength(0) + 1, x.GetLength(1) + 1)
        For i = 0 To x.GetLength(0)
            For j = 0 To x.GetLength(1)
                myx(i + 1, j + 1) = x(i, j)

Open in new window

AndyAinscowFreelance programmer / ConsultantCommented:
        ReDim myx(x.GetLength(0) + 1, x.GetLength(1) + 1)
        For i = 0 To x.GetLength(0)
            For j = 0 To x.GetLength(1)
                myx(i + 1, j + 1) = x(1, 1)

I think you will find the lower bound is 1 - hence your error at runtime.
lep1Author Commented:
Looks like there is an entry at MSDN about a mismatch between Excel and VB.NET array elements when transferring form Excel into a Visual Studio array, at URL:

There's about a dozen correspondences in the entry, so don't know if the problem was resolved or there is a work around.   Maybe an expert could review the comments, to strengthen the knowledge base at EE(?)
lep1Author Commented:
I think that QuickWatch shows a value for x(0,0) but it is really x(1,1), so why the mismatch?  Why would QuickWatch show values using a zero-based reference frame while the array is truly one-based?
All Courses

From novice to tech pro — start learning today.