神秘空参考

时间:2012-12-11 16:09:38

标签: vb.net

Imports System.IO
Imports System

Public Class Form1

    Private Class movie
        Public name As String
        Public actors() As String
        Public year As String
        Public country As String
        Public votes As String

    End Class

    Private movies(0) As movie
    Private fs As FileStream
    Private input As StreamReader

    Private Sub LoadToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadToolStripMenuItem.Click
        OpenFileDialog1.ShowDialog()
        Dim i As Integer = 0

        Dim currentmv As New movie
        Dim reader As String()


        If File.Exists(OpenFileDialog1.FileName) Then

            lblPath.Text = OpenFileDialog1.FileName
            Dim iFile As New StreamReader(lblPath.Text)

            While Not iFile.EndOfStream

                reader = iFile.ReadLine.Split(";")

                currentmv.name = reader(0)
                currentmv.actors = reader(1).Split(",")
                currentmv.year = reader(2)
                currentmv.country = reader(3)
                currentmv.votes = reader(4)

                lbMovies.Items.Add(currentmv.name)

                movies(i) = currentmv

                i = i + 1
                ReDim movies(i)

            End While
        End If

    End Sub

    Private Sub lbMovies_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbMovies.SelectedIndexChanged
        Dim i As Integer

        For i = 0 To movies(lbMovies.SelectedIndex).actors.Length

            lbActors.Items.Add(movies(lbMovies.SelectedIndex).actors(i))

        Next

    End Sub
End Class

当我请求长度时出现问题。它说这是一个空引用,但我看不出有任何错误。当我在一个对象数组中请求数组的长度时是否存在问题?

2 个答案:

答案 0 :(得分:3)

尝试

ReDim Preserve movies(i)

IIRC ReDim会将所有元素初始化为Nothing而不会Preserve

答案 1 :(得分:1)

使用数组存储数据并将其“名称”存储在列表框中是没有必要的。列表框不存储String,而是存储Object。您可以将数据存储在列表框本身中。

Private Class Data
    Public SomeNumber As Integer
    Public SomeString As String
    Public Overrides Function ToString() As String
        Return String.Format("{0} ({1})", SomeString, SomeNumber)
    End Function
End Class

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    ListBox1.Items.Add(New Data With {.SomeString = "John", .SomeNumber = 12})
    ListBox1.Items.Add(New Data With {.SomeString = "Paul", .SomeNumber = 36})
    ListBox1.Items.Add(New Data With {.SomeString = "Ringo", .SomeNumber = 48})

End Sub

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    Dim d As Data = DirectCast(sender, ListBox).SelectedItem
    If d IsNot Nothing Then
        Console.WriteLine(d.ToString)
    End If
End Sub

您将数据存储在列表中,bind将其存储到列表框中:

Private Class Data
    Public SomeNumber As Integer
    Public SomeString As String
    Public Overrides Function ToString() As String
        Return String.Format("{0} ({1})", SomeString, SomeNumber)
    End Function
End Class

Private datalist As New List(Of Data)

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    datalist.Add(New Data With {.SomeString = "John", .SomeNumber = 12})
    datalist.Add(New Data With {.SomeString = "Paul", .SomeNumber = 36})
    datalist.Add(New Data With {.SomeString = "Ringo", .SomeNumber = 48})

    ListBox1.DataSource = datalist

End Sub

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    Dim d As Data = DirectCast(sender, ListBox).SelectedItem
    If d IsNot Nothing Then
        Console.WriteLine(d.ToString)
    End If
End Sub