开发者

Why is this VB code failing on me?

开发者 https://www.devze.com 2023-02-15 00:38 出处:网络
Hers the form code: Imports TechSupportData Public Class frmOpenIncidents Private Sub frmOpenIncidents_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Hers the form code:

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

And heres the class its working with:

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
开发者_如何学Go

Heres the error I'm getting from Visual Studio:

Error 1 Value of type 'System.Collections.Generic.List(Of TechSupportData.IncidentDB)' cannot be converted to 'System.Collections.Generic.List(Of TechSupportData.Incident)'. C:\Users\Cody\Desktop\School\Current\ITECH4269 Visual Basic\Assignment2CodyStewart\Assignment2CodySteawrt\Assignment2CodySteawrt\frmOpenIncidents.vb 8 28 Assignment2CodySteawrt

Any guidance or suggestions would be much appreciated, thanks a lot.


In class frmOpenIncidents you've defined incidentList as a list of Incidents:

Dim incidentList As List(Of Incident)

then set it:

incidentList = IncidentDB.GetIncidents

But that IncidentDB method is defined as returning a list of Incident*DB*:

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

Solution might be to change the first definition to this?

Dim incidentList As List(Of IncidentDB)

You need to choose a name for the class - Incident or IncidentDB - and use it consistently to get rid of all similar errors.

0

精彩评论

暂无评论...
验证码 换一张
取 消