为什么这个VB代码失败了?

时间:2011-03-04 15:14:24

标签: vb.net

获取表单代码:

Imports TechSupportData

Public Class frmOpenIncidents

Private Sub frmOpenIncidents_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim incidentList As List(Of Incident)
    Try
        incidentList = IncidentDB.GetIncidents
        If incidentList.Count > 0 Then
            Dim incident As Incident
            For i As Integer = 0 To incidentList.Count - 1
                incident = incidentList(i)
                lvIncidents.Items.Add(incident.ProductCode)
                lvIncidents.Items(i).SubItems.Add(incident.DateOpened)
                lvIncidents.Items(i).SubItems.Add(incident.CustomerID)
                lvIncidents.Items(i).SubItems.Add(incident.TechID)
                lvIncidents.Items(i).SubItems.Add(incident.Title)
            Next
        Else
            MessageBox.Show("All Incidents are taken care of")
            Me.Close()
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message, ex.GetType.ToString)
        Me.Close()
    End Try
End Sub
End Class

继续与之合作的班级:

Imports System.Data.SqlClient

Public Class IncidentDB
    Public Shared Function GetIncidents() As List(Of IncidentDB)
        Dim incidentList As New List(Of IncidentDB)
        Dim connection As SqlConnection = TechSupportDB.GetConnection
        Dim selectStatement As String _
            = "SELECT CustomerID, ProductCode, TechID, DateOpened, Title" _
            & "FROM(Incidents)" _
            & "WHERE DateClosed IS NULL"
        Dim selectCommand As New SqlCommand(selectStatement, connection)
        Try
            connection.Open()
            Dim reader As SqlDataReader = selectCommand.ExecuteReader()
            Dim incident As Incident
            Do While reader.Read
                incident = New Incident
                incident.IncidentID = reader("IncidentID").ToString
                incident.CustomerID = reader("CustomerID").ToString
                incident.ProductCode = reader("ProductCode").ToString
                incident.DateOpened = reader("DateOpened").ToString
            Loop
            reader.Close()
        Catch ex As Exception
            Throw ex
        Finally
            connection.Close()
        End Try
        Return incidentList
    End Function
End Class

这是我从Visual Studio得到的错误:

错误1类型'System.Collections.Generic.List(Of TechSupportData.IncidentDB)'的值无法转换为'System.Collections.Generic.List(Of TechSupportData.Incident)'。 C:\ Users \ Cody \ Desktop \ School \ Current \ ITECH4269 Visual Basic \ Assignment2CodyStewart \ Assignment2CodySteawrt \ Assignment2CodySteawrt \ frmOpenIncidents.vb 8 28 Assignment2CodySteawrt

非常感谢任何指导或建议,非常感谢。

1 个答案:

答案 0 :(得分:1)

在课程frmOpenIncidents中,您已将incidentList定义为突发事件列表:

Dim incidentList As List(Of Incident)

然后设置它:

incidentList = IncidentDB.GetIncidents

但是,IncidentDB方法被定义为返回Incident * DB *列表:

Public Shared Function GetIncidents() As List(Of IncidentDB)

解决方案可能是将第一个定义更改为此?

Dim incidentList As List(Of IncidentDB)

您需要为类选择一个名称 - Incident或IncidentDB - 并始终使用它来消除所有类似的错误。