Solved

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

Posted on 2013-02-02
10
447 Views
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?
0
Comment
Question by:lep1
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 23

Expert Comment

by:Roopesh Reddy
ID: 38848359
Hi,

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?
0
 
LVL 78

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.
http://msdn.microsoft.com/en-us/library/a1y8b3b3%28v=vs.80%29.aspx
0
 
LVL 23

Expert Comment

by:Roopesh Reddy
ID: 38848413
Hi,

Object Array starts with 0th index - http://www.dotnetperls.com/object-array-vbnet

Are you using Collections object???
0
 
LVL 30

Expert Comment

by:hnasr
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.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 38848778
This works perfectly here:
zero based dimension
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:lep1
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
        Next
        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)
            Next
        Next

Open in new window

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 38848827
....
        MessageBox.Show(x.GetLowerBound(0))
        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)
            Next
        Next


I think you will find the lower bound is 1 - hence your error at runtime.
0
 

Author Comment

by:lep1
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:

http://social.msdn.microsoft.com/Forums/en-US/vbinterop/thread/8155cfa8-7b9f-4a55-ba46-45bddcb77779

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(?)
0
 

Author Comment

by:lep1
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?
0
 
LVL 44

Accepted Solution

by:
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.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now