【问题标题】:Index was outside bounds of array error whilst working with a ListBox使用 ListBox 时,索引超出了数组错误的范围
【发布时间】:2017-10-20 23:05:33
【问题描述】:

我正在尝试将数据从一个ListBox 导入另一个,同时消除文本中的逗号、引号和其他不必要的部分。

由于某种原因,当我尝试运行它并尝试传输数据时,我得到了错误:

索引超出了数组的范围。

这是我的代码:

Private Sub LoadButton_Click(sender As Object, e As EventArgs) Handles LoadButton.Click

    'check contents of list box
    If RawDataListBox.Items.Count = 0 Then
        MessageBox.Show("No rows in list box")
        Exit Sub
    End If

    ' split the data and store it in a structure
    Try

        Dim counterInteger As Integer = 1
        Dim AbsDataStructure(RawDataListBox.Items.Count) As absdata
        Dim FinalDataOutputString As String

        While counterInteger < RawDataListBox.Items.Count
            'read one line/row into a string
            Dim AbsSingleLineString As String = RawDataListBox.Items(counterInteger).ToString
            'Split string by comma
            Dim AbsDataString As String() = AbsSingleLineString.Split(","c) 'need to also get rid of the quotation marks on data

            With AbsDataStructure(counterInteger)
                .LocationNameString = AbsDataString(0)
                .TypeString = AbsDataString(1)
                .StateString = AbsDataString(2)
                .MeasureString = AbsDataString(3)
                .LocalGovernmentAreaString = AbsDataString(4)
                .TotalMalesInteger = Integer.Parse(AbsDataString(5))
                .TotalFemalesInteger = Integer.Parse(AbsDataString(6))
                .FrequencyString = AbsDataString(7)
                .PercentMalesDouble = Double.Parse(AbsDataString(8))
                .PercentFemalesDouble = Double.Parse(AbsDataString(9))
                .TotalMalesAndFemalesInteger = .TotalMalesInteger + .TotalFemalesInteger
            End With

            'BELOW BLOCK OF CODE DOESNT WORK FOR SOME REASON
            FinalDataOutputString = "Cities and Regions: " + AbsDataStructure(counterInteger).LocationNameString + ","
            FinalDataOutputString += "Type: " + AbsDataStructure(counterInteger).TypeString + ","
            FinalDataOutputString += "State: " + AbsDataStructure(counterInteger).StateString + ","
            FinalDataOutputString += "Females: " + AbsDataStructure(counterInteger).TotalFemalesInteger.ToString + ","
            FinalDataOutputString += "Males: " + AbsDataStructure(counterInteger).TotalMalesInteger.ToString + ","
            FinalDataOutputString += "%Female: " + AbsDataStructure(counterInteger).PercentFemalesDouble.ToString + ","
            FinalDataOutputString += "%Male: " + AbsDataStructure(counterInteger).PercentMalesDouble.ToString + ","
            FinalDataOutputString += "Total Males and Females: " + AbsDataStructure(counterInteger).TotalMalesAndFemalesInteger.ToString
            'add to line in listbox
            FinalDataListBox.Items.Add(FinalDataOutputString)

            counterInteger += 1
            'when counterinteger is 10 i get inputstring was not in correct format
        End While
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

【问题讨论】:

  • 你为什么不使用For Each
  • 什么部分?抱歉,我是伪语言的新手——我的课程才一个月。
  • “absdata”是类还是结构?
  • “absdata”是一个结构体。
  • 你为什么要使用它们的数组?它不会在其他任何地方使用,因为它是本地的。您可以在 while 循环中声明一个新实例并使用它。

标签: arrays vb.net listbox indexoutofboundsexception


【解决方案1】:

Visual Basic 是一种从零开始的索引语言。很难说这是否是问题所在,但我认为这可能是counterInteger 设置为1 的事实。当您启动AbsDataStructure 时,您将其大小设置为RawDataListBox.Items.Count。尝试将counterInteger 设置为0

【讨论】:

  • 感谢您的来信,我试过了,但错误仍然存​​在。
【解决方案2】:

您没有在方法之外使用该结构数组,因此我将其简化为:

Private Sub LoadButton_Click(sender As Object, e As EventArgs) Handles LoadButton.Click
    If RawDataListBox.Items.Count > 0 Then
        Dim FinalDataValues As New List(Of String)

        For Each rawData As String In RawDataListBox.Items
            Try
                Dim AbsDataString As String() = rawData.Split(","c)

                Dim TotalFemalesInteger As Integer = Integer.Parse(AbsDataString(6))
                Dim TotalMalesInteger As Integer = Integer.Parse(AbsDataString(5))
                Dim PercentMalesDouble As Double = Double.Parse(AbsDataString(8))
                Dim PercentFemalesDouble As Double = Double.Parse(AbsDataString(9))

                FinalDataValues.Clear()
                FinalDataValues.Add("Cities and Regions: " + AbsDataString(0))
                FinalDataValues.Add("Type: " + AbsDataString(1))
                FinalDataValues.Add("State: " + AbsDataString(2))
                FinalDataValues.Add("Females: " + TotalFemalesInteger.ToString)
                FinalDataValues.Add("Males: " + TotalMalesInteger.ToString)
                FinalDataValues.Add("%Female: " + PercentFemalesDouble.ToString)
                FinalDataValues.Add("%Male: " + PercentMalesDouble.ToString)
                FinalDataValues.Add("Total Males and Females: " + (TotalFemalesInteger + TotalMalesInteger).ToString)

                FinalDataListBox.Items.Add(String.Join(",", FinalDataValues.ToArray))
            Catch ex As Exception
                MessageBox.Show("Line: " & rawData & vbCrLf & vbCrLf & ex.ToString, "Error Parsing Line")
            End Try
        Next
    Else
        MessageBox.Show("No rows in list box")
    End If
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 1970-01-01
    • 2014-03-09
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多