指数是出界。 For循环和数组 [英] Index was out of bounds. For Loops and Arrays

查看:199
本文介绍了指数是出界。 For循环和数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建两个数组在数据库中弥漫着一组字符串的两个表,然后对它们进行比较。例如: ARRAY1 [0] =1101 ARRAY2 [0] =0110

如果双方各自的字符等于1,则执行操作。但是,当我运行此code我收到的错误:索引是数组的界限之外。 System.IndexOutOfRangeException。

由于某种原因,我相信问题方面与以下两个语句:

comparestringa = userintarray(X) comparestringb = eventintarray(X)

当我把它们注释掉,误差不显示。

  myconnect =新的SqlConnection(XXXX)
    表1昏暗作为新的SqlCommand(SELECT * from table1中,myconnect)
    昏暗表2作为新的SqlCommand(SELECT * FROM表2,myconnect)

    昏暗ARRAY1作为新的ArrayList
    昏暗ARRAY2作为新的ArrayList

    Table1.Connection.Open()
    昏暗为r SqlDataReader的
    R = Table1.ExecuteReader(CommandBehavior.CloseConnection)
    虽然r.Read
        array1.Add(R(1)的ToString())
        array1.Add(R(2)的ToString())
        array1.Add(R(3)的ToString())
        array1.Add(R(4)的ToString())

    结束在
    r.Close()
    myconnect.Close()



    Table2.Connection.Open()
    昏暗的R2作为SqlDataReader的
    R2 = Table2.ExecuteReader(CommandBehavior.CloseConnection)
    虽然r2.Read
        array2.Add(R2(1)的ToString())
        array2.Add(R2(2)的ToString())
        array2.Add(R2(3)的ToString())
        array2.Add(R2(4)的ToString())

    结束在
    r2.Close()
    myconnect.Close()



    昏暗的comparestringa,comparestringb作为字符串
    昏暗compare_string_counter作为的Int16 = 1

    comparestringa = userintarray(0)
    comparestringb = eventintarray(0)


     对于x = 0到array1.Count  -  1

        如果comparestringa(X)=1并且comparestringa(x)的= comparestringb(x)的再

              Label4.Text = Label4.Text +1

        其他
            compare_string_counter = compare_string_counter + 1
        结束如果
        comparestringa = userintarray(X)
        comparestringb = eventintarray(X)

    下一个
 

解决方案

userintarray和eventintarray没有声明(至少不是在code您发布。)显然,他们的尺寸太小,否则他们是不会标注在所有的。

I'm trying to create two arrays filled with a set of strings from two tables in a database and then compare them. For example: array1[0]="1101" and array2[0]="0110"

If both respective characters equals 1 then perform an action. But when I run this code I receive the error: Index was outside the bounds of the array. System.IndexOutOfRangeException.

For some reason I believe the problem area is with the following two statements:

comparestringa = userintarray(x) and comparestringb = eventintarray(x)

When i comment them out, the error doesn't show.

    myconnect = New SqlConnection("xxxx")
    Dim Table1 As New SqlCommand("SELECT * FROM Table1", myconnect)
    Dim Table2 As New SqlCommand("SELECT * FROM Table2", myconnect)

    Dim array1 As New ArrayList
    Dim array2 As New ArrayList

    Table1.Connection.Open()
    Dim r As SqlDataReader
    r = Table1.ExecuteReader(CommandBehavior.CloseConnection)
    While r.Read
        array1.Add(r(1).ToString())
        array1.Add(r(2).ToString())
        array1.Add(r(3).ToString())
        array1.Add(r(4).ToString())

    End While
    r.Close()
    myconnect.Close()



    Table2.Connection.Open()
    Dim r2 As SqlDataReader
    r2 = Table2.ExecuteReader(CommandBehavior.CloseConnection)
    While r2.Read
        array2.Add(r2(1).ToString())
        array2.Add(r2(2).ToString())
        array2.Add(r2(3).ToString())
        array2.Add(r2(4).ToString())

    End While
    r2.Close()
    myconnect.Close()



    Dim comparestringa, comparestringb As String
    Dim compare_string_counter As Int16 = 1

    comparestringa = userintarray(0)
    comparestringb = eventintarray(0)


     For x = 0 To array1.Count - 1

        If comparestringa(x) = "1" And comparestringa(x) = comparestringb(x) Then

              Label4.Text = Label4.Text + " 1 "

        Else
            compare_string_counter = compare_string_counter + 1
        End If
        comparestringa = userintarray(x)
        comparestringb = eventintarray(x)

    Next

解决方案

userintarray and eventintarray are not declared (at least not in the code you posted.) Apparently they are dimensioned too small, or they are not dimensioned at all.

这篇关于指数是出界。 For循环和数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆