VB.NET 分配值后属性似乎为空



我有一个窗口窗体,在其中我调用另一个窗体来执行一些操作:

Private Sub btnAddParticipants_Click(sender As Object, e As EventArgs) Handles btnAddParticipants.Click
    Dim addparticipants As New frmAddParticipants()
    addparticipants.movie.movieID = Split(lstSelection.SelectedItem.ToString(), " ")(0)
    Me.Hide()
    frmAddParticipants.Show()
End Sub

当我在 lstSelection 中分配值以addparticipants.movie.movieID调试器显示分配正确时,但是当我输入新的表单代码时,此属性Null

我在这里包括frmAddParticipants表格:

Public Class frmAddParticipants
    Private person As New Person()
    Private _movie As New Movie()
    Private role As New Role()
    Public Property movie As Movie
        Get
            Return _movie
        End Get
        Set(value As Movie)
            _movie = value
        End Set
    End Property
    Private Sub frmAddParticipants_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            person.readAllPeople()
        Catch ex As OleDbException
            MessageBox.Show(ex.ToString())
            Exit Sub
        End Try
        For Each listperson As Person In person.personDAO.peopleList
            lstPeople.Items.Add(listperson.personID + " " + listperson.name + " " + listperson.surname)
        Next
        Try
            role.readAllRoles()
        Catch ex As OleDbException
            MessageBox.Show(ex.ToString())
            Exit Sub
        End Try
        For Each listrole As Role In role.roleDAO.rolesList
            lstRoles.Items.Add(listrole.roleID + " " + listrole.description)
        Next
    End Sub
    Private Sub lstPeople_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstPeople.SelectedIndexChanged
        person.personID = Split(lstPeople.SelectedItem.ToString(), " ")(0)
        Try
            person.readPerson()
        Catch ex As OleDbException
            MessageBox.Show(ex.ToString())
            Exit Sub
        End Try
        txtInfo.Text = person.name.ToString() + " " + person.surname.ToString()
    End Sub
    Private Sub lstRoles_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstRoles.SelectedIndexChanged
        role.roleID = Split(lstRoles.SelectedItem.ToString(), " ")(0)
        Try
            role.readRole()
        Catch ex As OleDbException
            MessageBox.Show(ex.ToString())
            Exit Sub
        End Try
        txtInfo.Text = role.description.ToString()
    End Sub
    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim returning As Integer
        Try
            returning = movie.insertParticipant(person, role)
        Catch ex As OleDbException
            MessageBox.Show(ex.ToString())
            Exit Sub
        End Try
        If returning <> 0 And returning <> -1 Then
            MessageBox.Show("Participant added succesfully")
        End If
    End Sub
    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        Try
            If movie.deleteParticipant(person, role) = 0 Then
                MessageBox.Show("No participant deleted as it did not exist on the database previously")
            Else
                MessageBox.Show("Participant deleted succesfully")
            End If
        Catch ex As OleDbException
            MessageBox.Show(ex.ToString())
        End Try
    End Sub
End Class

"奇怪"的是,我有另一种类似的形式,它是frmAddFilms工作的精确副本。我在这里包含代码以防万一:

Public Class frmAddFilms    
    Private _person As New Person()
    Private movie As New Movie()
    Private role As New Role()
    Public Property person As Person
        Get
            Return _person
        End Get
        Set(value As Person)
            _person = value
        End Set
    End Property
    Private Sub frmAddFilms_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            movie.readAllMovies()
        Catch ex As OleDbException
            MessageBox.Show(ex.ToString())
            Exit Sub
        End Try
        For Each listmovie As Movie In movie.movieDAO.moviesList
            lstFilms.Items.Add(listmovie.movieID + " " + listmovie.title)
        Next
        Try
            role.readAllRoles()
        Catch ex As OleDbException
            MessageBox.Show(ex.ToString())
            Exit Sub
        End Try
        For Each listrole As Role In role.roleDAO.rolesList
            lstRoles.Items.Add(listrole.roleID + " " + listrole.description)
        Next
    End Sub
    Private Sub lstFilms_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstFilms.SelectedIndexChanged
        movie.movieID = Split(lstFilms.SelectedItem.ToString(), " ")(0)
        Try
            movie.readMovie()
        Catch ex As OleDbException
            MessageBox.Show(ex.ToString())
            Exit Sub
        End Try
        txtInfo.Text = movie.title.ToString() + vbCrLf + vbCrLf + "(" + movie.releaseYear.ToString() + ")"
    End Sub
    Private Sub lstRoles_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstRoles.SelectedIndexChanged
        role.roleID = Split(lstRoles.SelectedItem.ToString(), " ")(0)
        Try
            role.readRole()
        Catch ex As OleDbException
            MessageBox.Show(ex.ToString())
            Exit Sub
        End Try
        txtInfo.Text = role.description.ToString()
    End Sub
    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim returning As Integer
        Try
            returning = person.insertFilm(movie, role)
        Catch ex As OleDbException
            MessageBox.Show(ex.ToString())
            Exit Sub
        End Try
        If returning <> 0 And returning <> -1 Then
            MessageBox.Show("Participation added successfully")
        End If
    End Sub
    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        Try
            If person.deleteFilm(movie, role) = 0 Then
                MessageBox.Show("No participation deleted as it did not exist previously")
            Else
                MessageBox.Show("Participation deleted successfully")
            End If
        Catch ex As OleDbException
            MessageBox.Show(ex.ToString())
        End Try
    End Sub
End Class

而且我还包括了它的称呼方式,与另一个完全相同:

Private Sub btnAddFilms_Click(sender As Object, e As EventArgs) Handles btnAddFilms.Click
    Dim addfilms As New frmAddFilms()
    addfilms.person.personID = Split(lstSelection.SelectedItem.ToString(), " ")(0)
    Me.Hide()
    addfilms.Show()
End Sub

我已经检查了大约一千次代码,但我没有发现两者之间有任何区别,这可能会导致此错误发生

问题是代码中的拼写错误。替换此行

frmAddParticipants.Show()

与您打算打电话的人

addparticipants.Show()

调用 frmAddParticipants.Show 会导致调用 My.Forms.frmAddParticipants 属性,该属性创建 frmAddTParticipants 窗体的新实例。这个新实例不知道您的电影 ID。

相关内容

  • 没有找到相关文章

最新更新