Solved

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

Posted on 2013-02-02
10
449 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 79

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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
 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

815 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

13 Experts available now in Live!

Get 1:1 Help Now