Link to home
Start Free TrialLog in
Avatar of triphen
triphen

asked on

VB.net Index was out of range?

Hey guys,

I can't figure out why I am getting this error? Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: Index VS2010 VB.net

      Dim RC As Integer = dgv_POSData.RowCount
        Dim i As Integer = 0
        txt_Log.Text = txt_Log.Text & "OpenDates " & lbl_SrchStartDate.Text & " and " & lbl_SrchEndDate.Text & vbCrLf
        txt_Log.Text = txt_Log.Text & "Verifying " & RC & " rows" & vbCrLf

        Do While i <= RC
            'MessageBox.Show(i)

            Dim HN As String = dgv_POSData.Rows(i).Cells(2).Value
            Dim DN As String = dgv_POSData.Rows(i).Cells(3).Value
            Dim HF As String = dgv_POSData.Rows(i).Cells(4).Value
            Dim HPF As String = dgv_POSData.Rows(i).Cells(5).Value

            If HN - DN = 0 Then
                txt_Log.Text = txt_Log.Text & "Trans " & dgv_POSData.Rows(i).Cells(1).Value & " Header vs Detail = Good" & vbCrLf
            Else
                Dim RD As Double = HN - DN
                txt_Log.Text = txt_Log.Text & "Trans " & dgv_POSData.Rows(i).Cells(1).Value & " Header vs Detail difference of " & RD & vbCrLf
            End If

            If HF - HPF = 0 Then
                txt_Log.Text = txt_Log.Text & "Trans " & dgv_POSData.Rows(i).Cells(1).Value & " Header vs HowPaid = Good" & vbCrLf
            Else
                Dim RD As Double = HF - HPF
                txt_Log.Text = txt_Log.Text & "Trans " & dgv_POSData.Rows(i).Cells(1).Value & " Header vs HowPaid difference of " & RD & vbCrLf
            End If


            i = i + 1
        Loop

Open in new window



For testing I added a messagebox to show me the value of i. If I uncomment MessageBox.Show(i) it works and fails on the last row of the datagrid, but if I comment it out, it fails on the very first row? I don't understand what displaying the value of i has to do with anything?

What am I missing? Am I going crazy?
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of triphen
triphen

ASKER

That may have worked, but now I have another problem with my SQL it looks like be right back :)
*No points*

Just to illustrate what Fernando has stated.  Say I have a collection (or an array) of 5 items.

The count property will return 5.
But the items in the collection/array are accessible by their index.
Arr[0]
Arr[1]
Arr[2]
Arr[3]
Arr[4]

Five items but no index for 5.  So when iterating over a collection based on the count you want to use either count - 1; e.g. -
Module Module1
	Sub Main()
		Dim Arr(4) As Integer
		For i As Integer = 0 To 4
			Arr(i) = i
		Next

		Console.WriteLine("Array Count is {1};{0}Array Length is {2};{0}Array Lower Bounds is {3};{0}Array Upper Bounds is {4};", Environment.NewLine, Arr.Count, Arr.Length, Arr.GetLowerBound(0), Arr.GetUpperBound(0))
		Dim j As Integer = 0
		While (j <= Arr.Count - 1)
			Console.WriteLine("Array Index {0}; contains {1}", j, Arr(j))
			j = j + 1
		End While
		Console.ReadLine()
	End Sub
End Module

Open in new window

Or the count explicitly -
Module Module1
	Sub Main()
		Dim Arr(4) As Integer
		For i As Integer = 0 To 4
			Arr(i) = i
		Next

		Console.WriteLine("Array Count is {1};{0}Array Length is {2};{0}Array Lower Bounds is {3};{0}Array Upper Bounds is {4};", Environment.NewLine, Arr.Count, Arr.Length, Arr.GetLowerBound(0), Arr.GetUpperBound(0))
		Dim j As Integer = 0
		While (j < Arr.Count)
			Console.WriteLine("Array Index {0}; contains {1}", j, Arr(j))
			j = j + 1
		End While
		Console.ReadLine()
	End Sub
End Module

Open in new window

Both of the code examples above produce the following output -User generated image-saige-