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

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
  • 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 80

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 - http://www.dotnetperls.com/object-array-vbnet

Are you using Collections object???
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

828 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