索引超出了数组VB 2015的范围

时间:2018-06-08 19:03:26

标签: vb.net

我收到此错误消息,并指向此行

  

索引超出了数组的范围

     

bookinfo(i).Author = parts(1)

有人能帮我理解我做错了什么吗?

谢谢!

   Private Sub JamesProject1_Load(sender As Object, e As EventArgs) Handles 
    MyBase.Load
    Dim parts() As String
    lines = IO.File.ReadAllLines("LibraryDatabase.txt")


    ReDim Preserve bookinfo(lines.Count)

    If Not OpenFileDialog1.ShowDialog() = DialogResult.OK Then
        MessageBox.Show("You must choose a file")
        Me.Close()
    End If

    lines = IO.File.ReadAllLines(OpenFileDialog1.FileName)


    For i = 0 To lines.Count - 1
        parts = lines(i).Split(","c)
        bookinfo(i).Title = parts(0)
        bookinfo(i).Author = parts(1)
        bookinfo(i).ISBN = parts(2)
        bookinfo(i).YearPublished = parts(3)
    Next

    Dim query = From book In bookinfo
                Order By book.Title, book.Author, book.ISBN, book.YearPublished
                Select book.Title, book.Author, book.ISBN, book.YearPublished

    DGVBookInfo.DataSource = query.ToList

    DGVBookInfo.Columns("Title").HeaderText = "Title"
    DGVBookInfo.Columns("Author").HeaderText = "Author"
    DGVBookInfo.Columns("ISBN").HeaderText = "ISBN"
    DGVBookInfo.Columns("Year Published").HeaderText = "Year Published"
    DGVBookInfo.AutoResizeColumns()


    DGVBookInfo.RowHeadersVisible = False



End Sub

0 个答案:

没有答案