ArgumentOutOfRangeException错误

时间:2013-11-25 17:47:42

标签: vb.net exception-handling visual-studio-2013 outofrangeexception

我正在尝试创建一个处理医院病人档案的应用程序。 它给了我一个错误并告诉我 PatientView = PatientCollection(counter) - ArgumentOutOfRangeException未处理。这是什么意思,我该如何解决这个问题?

Public Class SelectPatient

Private Sub SelectPatient_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Label1.Text = "Select Patient"

    Dim counter As Integer = 0

    ComboBox1.Items.Clear()
    For counter = 0 To PatientCollection.Count
        Dim PatientView As New PatientObject4
        PatientView = PatientCollection(counter)
        ComboBox1.Items.Add(PatientView.LastName & "," & PatientView.Firstname)
    Next
End Sub

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged

    CollectionIndexValue = ComboBox1.SelectedIndex

    Globals.NewPatientData()



End Sub
End Class

非常感谢你的时间。非常感谢

1 个答案:

答案 0 :(得分:4)

.NET Framework中的数组从索引0开始,以Count-1结束。

在你的循环中

For counter = 0 To PatientCollection.Count

你在Count处停留,因此counter假设的最后一个值无效 您需要将该循环更改为

For counter = 0 To PatientCollection.Count - 1

数组的Count属性表示数组中包含的项目数。因此,如果第一个索引为零,则最后一个索引应为Count - 1