Vb.net 为什么这个VB代码对我来说失败了?
表格代码:Vb.net 为什么这个VB代码对我来说失败了?,vb.net,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 = I
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(属于TechSupportData.IncidentDB)”类型的值转换为“System.Collections.Generic.List(属于TechSupportData.Incident)”。C:\Users\Cody\Desktop\School\Current\ITECH4269 Visual Basic\Assignment2CodyStewart\Assignment2CodySteawrt\Assignment2CodySteawrt\FRMOpenicients.vb 8 28 Assignment2CodySteawrt
非常感谢您提供的任何指导或建议。在FRMOpenicients课程中,您已将事件列表定义为事件列表:
Dim incidentList As List(Of Incident)
然后设置它:
incidentList = IncidentDB.GetIncidents
但是IncidentDB方法被定义为返回事件*DB*的列表:
Public Shared Function GetIncidents() As List(Of IncidentDB)
解决方案可能是将第一个定义更改为
Dim incidentList As List(Of IncidentDB)
您需要为类选择一个名称—Incident或IncidentDB—并一致使用它来消除所有类似的错误。这解决了该错误,谢谢!现在我遇到了这个问题:“TechSupportData.IncidentDB”类型的错误1值无法转换为“TechSupportData.Incident”。C:\Users\Cody\Desktop\School\Current\ITECH4269 Visual Basic\Assignment2CodyStewart\Assignment2CodySteawrt\Assignment2CodySteawrt\frmopeIncidents。vb 12 32 Assignment2Codysteawr类似于它的另一种方式:应该是
公共共享函数GetIncidents()作为(事件)列表。
我怀疑。@Filburt-听起来很合理。@Cody-我添加了一个额外的注释。您似乎混合了对IncidentDB类和Incident类的引用。您可能只想使用其中一个,或者在两个之间有一个明确定义的转换方式,如果您需要这两个原因,这在发布的代码中并不明显。也可以考虑不同的命名空间,这样您就可以使用前缀来保持数据类的区别。