分配值

时间:2018-04-18 11:09:09

标签: database vb.net null

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

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

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

1 个答案:

答案 0 :(得分:1)

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

frmAddParticipants.Show()

与你打算打电话的人

addparticipants.Show()

调用frmAddParticipants.Show会调用My.Forms.frmAddParticipants属性,该属性会创建frmAddParticipants表单的新实例。而这个新实例对你的电影ID一无所知。