Problem with array loops

Results 1 to 2 of 2

Thread: Problem with array loops

  1. #1
    Join Date
    Dec 1969

    Default Problem with array loops

    I have the following code that produces an error. <BR><BR>Dim dbconn1 As SqlConnection = New SqlConnection("server=AM1ST_FS1;database=HRINFO;ui d=sa;")<BR> Dim selectCMD3 As SqlCommand = New SqlCommand()<BR> selectCMD3.CommandText = "Select monthnumber from timemonths where loginID = &#039;" & Session("whosonline") & "&#039;"<BR> selectCMD3.Connection = dbconn1<BR> selectCMD3.CommandTimeout = 30<BR><BR> Dim data1 as SqlDataReader = selectCMD3.ExecuteReader()<BR> Dim mymonths as ArrayList<BR> mymonths = new ArrayList()<BR> do while data1.Read()<BR> mymonths.Add(data1("monthnumber"))<BR> loop<BR> <BR> Dim selectCMD4 As SqlCommand = New SqlCommand()<BR> selectCMD4.CommandText = "Select distinct monthnumber from timemonths"<BR> selectCMD4.Connection = dbconn<BR> selectCMD4.CommandTimeout = 30<BR><BR> Dim data2 as SqlDataReader = selectCMD4.ExecuteReader()<BR> Dim totalmonths as ArrayList<BR> totalmonths = new ArrayList()<BR> do while data2.Read()<BR> totalmonths.Add(data2("monthnumber"))<BR> loop<BR><BR> <BR> <BR> Dim matchs as boolean<BR> Dim i as Integer = 0<BR> Dim j as integer = 0<BR> Dim z as integer = 0<BR> &#039;Dim monthnames as array<BR> dim start as object<BR> dim middle as object<BR> Dim monthnames as ArrayList<BR> monthnames = new ArrayList()<BR> <BR> <BR> <BR> For each start in totalmonths<BR> matchs = false<BR> if totalmonths(j) = DateTime.Now.Month then<BR> &#039;exit For<BR> end if<BR> For each middle in mymonths<BR> if totalmonths(j) = mymonths(z) then &#039; This line produces error.<BR> matchs = True<BR> z = z + 1 <BR> End if<BR> Next<BR> if matchs = False Then<BR> monthnames.Add(totalmonths)<BR> i = i + 1<BR> j = j + 1<BR> End if <BR> <BR> Next<BR><BR>Here is the error. Any help would be great. <BR><BR>Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index <BR>Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. <BR><BR>Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index<BR><BR>Source Error: <BR><BR><BR>Line 113: end if<BR>Line 114: For each middle in mymonths<BR>Line 115: if totalmonths(j) = mymonths(z) then<BR>Line 116: matchs = True<BR>Line 117: z = z + 1 <BR> <BR>

  2. #2
    Join Date
    Dec 1969

    Default RE: Problem with array loops

    I didn&#039;t go through the code line by line, but wouldn&#039;t you have to reset your z value every time you started another loop in your outer loop "For each start in totalmonths"? It looks like z can keep being incrimented when a match is found in the inner loop, but is not reset to 0 when the outter loop starts again.<BR><BR>I think adding a reset on the z value on the outer loop may prevent an out or range error"<BR><BR>For each start in totalmonths<BR>matchs = false<BR>z=0<BR>etc<BR>etc<BR><BR>Good luck<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts