无法将类型为'System.Collections.Generic.List`1 ['的对象强制转换为'System.Collections.Generic.IEnumerable`1 ['

时间:2015-06-26 23:10:51

标签: vb.net list class

我在运行时得到的错误是

Unable to cast object of type
'System.Collections.Generic.List`1[TCPClientClean.ChatClient+LogTime]'
to type
'System.Collections.Generic.IEnumerable`1[TCPClientClean.TimeLogLabel+LogTime]'.

我有一个用于记录事件和保存时间戳的类。然后我想保存在TimeLogLabel类中创建的列表,然后在启动时重新加载。保存到文本文件已解决并且有效,但我不了解如何将保存的列表恢复到TimeLogLabel类。

Option Strict On
Imports System.Text
Imports System.IO
Imports System.Collections.Generic
Public Class TimeLogLabel
Inherits Label

Private m_VaraibleToSet As Boolean
<System.ComponentModel.Category("Control")> _
Public Property VaraibleToSet() As Boolean
    Get
        Return m_VaraibleToSet
    End Get
    Set(ByVal value As Boolean)
        m_VaraibleToSet = value
    End Set
End Property
Private m_PrefixText As String = "Prefix Text"
<System.ComponentModel.Category("Control")> _
Public Property PrefixText() As String
    Get
        Return m_PrefixText
    End Get
    Set(ByVal value As String)
        m_PrefixText = value
    End Set
End Property
Public Class LogTime
    Private m_EventName As String
    Public Property EventName() As String
        Get
            Return m_EventName
        End Get
        Set(ByVal value As String)
            m_EventName = value
        End Set
    End Property
    Private m_StartT As String
    Public Property StartT() As String
        Get
            Return m_StartT
        End Get
        Set(ByVal value As String)
            m_StartT = value
        End Set
    End Property
    Private m_StopT As String
    Public Property StopT() As String
        Get
            Return m_StopT
        End Get
        Set(ByVal value As String)
            m_StopT = value
        End Set
    End Property
    Private m_TSpan As String
    Public Property TSpan() As String
        Get
            Return m_TSpan
        End Get
        Set(ByVal value As String)
            m_TSpan = value
        End Set
    End Property
    Public Sub New( _
  ByVal m_EventName As String, _
  ByVal m_StartT As String, _
  ByVal m_StopT As String, _
  ByVal m_TSpan As String)
        EventName = m_EventName
        StartT = m_StartT
        StopT = m_StopT
        TSpan = m_TSpan
    End Sub
End Class


Public TimeList As List(Of LogTime) = New List(Of LogTime)

    Public Sub StartTimer()
    If Ons = False Then
        Ons = True
        EventIdInt = EventIdInt + 1
        EventIdStg = ""
        If EventIdInt <= 9 Then
            EventIdStg = "0" & CStr(EventIdInt)
        ElseIf EventIdInt >= 9 Then
            EventIdStg = CStr(EventIdInt)
        End If
        StartTime = Now
        TimeList.Add(New LogTime(PrefixText & " " & EventIdStg,     "StartTime " & StartTime, "", ""))
        Timer.Enabled = True
        Timer.Start()
    End If
End Sub

Public Sub StopTimer()
    If Ons = True Then
        Ons = False
        EventIdInt = EventIdInt + 1
        EventIdStg = ""
        If EventIdInt <= 9 Then
            EventIdStg = "0" & CStr(EventIdInt)
        ElseIf EventIdInt >= 9 Then
            EventIdStg = CStr(EventIdInt)
        End If
        StopTime = Now
        TimeList.Add(New LogTime(PrefixText & " " & EventIdStg, "", _           "Stop Time " & StopTime, " Up Time " & AccTimeStg))
        Timer.Enabled = False
        Timer.Stop()
    End If
End Sub






Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Collections.Generic
Imports System.Linq  
Public Class ChatClient   ' Form that has "TimeLogLabel" on the form


Public Class LogTime
    Private m_EventName As String
    Public Property EventName() As String
        Get
            Return m_EventName
        End Get
        Set(ByVal value As String)
            m_EventName = value
        End Set
    End Property
    Private m_StartT As String
    Public Property StartT() As String
        Get
            Return m_StartT
        End Get
        Set(ByVal value As String)
            m_StartT = value
        End Set
    End Property
    Private m_StopT As String
    Public Property StopT() As String
        Get
            Return m_StopT
        End Get
        Set(ByVal value As String)
            m_StopT = value
        End Set
    End Property
    Private m_TSpan As String
    Public Property TSpan() As String
        Get
            Return m_TSpan
        End Get
        Set(ByVal value As String)
            m_TSpan = value
        End Set
    End Property
    Public Sub New( _
  ByVal m_EventName As String, _
  ByVal m_StartT As String, _
  ByVal m_StopT As String, _
  ByVal m_TSpan As String)
        EventName = m_EventName
        StartT = m_StartT
        StopT = m_StopT
        TSpan = m_TSpan
    End Sub
End Class

Private Sub ChatClient_Load(ByVal sender As System.Object, ByVal e s    System.EventArgs) Handles MyBase.Load

Dim TimeListSaved As List(Of LogTime) = New List(Of LogTime)
    ' Dim TimeListSaved As IEnumerable(Of LogTime) = New List(Of LogTime)

    Dim Sartuptime As DateTime = Now
    TimeListSaved.Add(New LogTime("Prefix&EvebtId 01", "Start Time " & UpStartTime, "Stop Time", "Up Time"))

LAtSvrComm.TimeList.AddRange(TimeListSaved)
' this is where I get my error
    ' Unable to cast object of type 
    'System.Collections.Generic.List`1[TCPClientClean.ChatClient+LogTime]'
    'to type 
    'System.Collections.Generic.IEnumerable`1[TCPClientClean.TimeLogLabel+LogTime]'.
    End Sub

Private Sub ChatClient_FormClosing(ByVal sender As System.Object, ByVal e As 

System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing


Dim FILE_NAME As String = "C:\LogSave.txt"
    Dim objWriter As New System.IO.StreamWriter(FILE_NAME)

    For Each TimeLogLabel In LAtSvrComm.TimeList
        objWriter.WriteLine(TimeLogLabel.EventName & " * " &     TimeLogLabel.StartT & " * " & TimeLogLabel.StopT & _
                            " * " & TimeLogLabel.TSpan)
    Next TimeLogLabel
    objWriter.WriteLine("end of " & LAtSvrComm.PrefixText)
    End Sub



    objWriter.Close()
end sub

2 个答案:

答案 0 :(得分:0)

你声明了2个名为LogTime的类,而不是1.它们是成员类,并且它们之间不能自动转换,因此CLR不知道如何为你转换(抛出此错误) )。

也就是说,TCPClientClean.ChatClient+LogTimeTCPClientClean.TimeLogLabel+LogTime 不是相同的类型。

因为看起来你打算只有一个LogTime类,所以将它声明为一个单独的类(而不是作为另一个类的成员的类)并删除2个不同的{{1}你现在有。

答案 1 :(得分:0)

这解决了它。 在表单加载时,我将读取文本文件,然后将字符串放入正确的项目。

      LAtSvrComm.TimeList.Add(New TimeLogLabel.LogTime("Prefix&EvebtId 01", "Start Time " & UpStartTime, "Stop Time", "Up Time"))
@Billy N感谢我指出了正确的方向。通过解释它的方式让我想出了这个解决方案。