system.indexoutofrangeexception

eprme

New member
Joined
May 11, 2005
Messages
1
Programming Experience
10+
Hi. Can anyone help. I have a situation where I have read the contents of an excel range into a an array. When I then try to loop round that array starting at (0,0) I get the System.IndexOutOfRangeException. However, when running my program in debug mode I can watch the contents of the (0,0) and see that they are valid. Code looks a bit like this:

Dim xIncomes(30,11)
Dim x as integer

xIncomes = Excelwrkbk.ActiveSheet.Range("F70:p93").value
...
for x = 0 to 11
if xIncomes(0,x) = "whatever" then
...
end if
next

The exception is raised on the if statement. In debug mode I can see the contents of xIncomes(0,0)
 
for x = 0 to 10

xIncomes(30,11) means there are 11 elements, being 0 through 10

by having it: for x = 0 to 11 that's an 11th element that's not there, henace the out of range exception
 
Btw, you could check last index in the second dimension through getUpperBound method ... and you'll never have situation like this one.

Dim xIncomes(30,11) As Integer

Console.WriteLine(xIncomes.GetUpperBound(1))

actually:
MessageBox.Show(xIncomes.GetUpperBound(1))


and remember that arrays in vb.net are always zero-based ... no matter we like it or not

Cheers :)
 
Back
Top