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

Posted on 2013-02-02
Last Modified: 2013-02-03
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?
Question by:lep1
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
LVL 23

Expert Comment

by:Roopesh Reddy
ID: 38848359

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?
LVL 82

Expert Comment

by:David Johnson, CD, MVP
ID: 38848390
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.
LVL 23

Expert Comment

by:Roopesh Reddy
ID: 38848413

Object Array starts with 0th index -

Are you using Collections object???

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 30

Expert Comment

ID: 38848453
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.
LVL 44

Expert Comment

ID: 38848778
This works perfectly here:
zero based dimension

Author Comment

ID: 38848798
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

LVL 44

Expert Comment

ID: 38848827
        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.

Author Comment

ID: 38848830
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(?)

Author Comment

ID: 38848838
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?
LVL 44

Accepted Solution

AndyAinscow earned 100 total points
ID: 38849032
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.

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : All lightning effects with instructions : http://www.mediaf…

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question