Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15744

Unable to cast object of type to type IEnumerable

$
0
0
Hi folks,

I am buidling this app where I use deserialization to obtain a List of the object "Contact", so far so good. When I try to retrieve the first element from this obtained list (which I sorted and declared in "objsortedData" I get this InvalidCastException:

{System.InvalidCastException: Unable to cast object of type 'Contact' to type 'System.Collections.IEnumerable'. at AdresboekApp.MainPage..ctor()}

See my code below, is it necessary to cast to IEnumerable? And if so, how do I correctly cast my list(values) so that I can use indexation to declare the "firstPerson" variable?

Main Sub:
Code:

    Public Sub New()
        InitializeComponent()

        SupportedOrientations = SupportedPageOrientation.Portrait Or SupportedPageOrientation.Landscape

        'Save default persondata to Contacten.xml file on local device
        Dim xmlWriterSettings As XmlWriterSettings = New XmlWriterSettings()
        xmlWriterSettings.Indent = True

        Dim local As System.IO.IsolatedStorage.IsolatedStorageFile = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication()
        If Not local.DirectoryExists("\Assets") Then local.CreateDirectory("\Assets")

        Using stream As IsolatedStorageFileStream = local.OpenFile("\Assets\Contacten.xml", FileMode.Create)
            Dim serializer As XmlSerializer = New XmlSerializer(GetType(List(Of Contact)))

            Using xmlWriter As XmlWriter = xmlWriter.Create(stream, xmlWriterSettings)
                serializer.Serialize(xmlWriter, GeneratePersonData())
            End Using
        End Using

        'Show first contact in Contacten.xml file on local device
        Using stream As IsolatedStorageFileStream = local.OpenFile("\Assets\Contacten.xml", FileMode.Open)
            Dim serializer As XmlSerializer = New XmlSerializer(GetType(List(Of Contact)))
            Dim persondata As List(Of Contact) = CType(serializer.Deserialize(stream), List(Of Contact))
            Dim objsortedData = persondata.OrderBy(Function(t) CStr(t.Firstname))
            Dim firstPerson = objsortedData(0)

            Me.listBox.ItemsSource = firstPerson
        End Using


    End Sub

Function GeneratePersonData wich generates a default list of Contact
Code:

'Generate default persondata
    Private Function GeneratePersonData() As List(Of Contact)
        Dim persondata As List(Of Contact) = New List(Of Contact)()
        persondata.Add(New Contact() With {
            .Firstname = "Louis",
            .Lastname = "van Gaal",
            .Emailadress = "louisvangaal@prive.nl"
        })
        persondata.Add(New Contact() With {
            .Firstname = "Patrick",
            .Lastname = "Kluivert",
            .Emailadress = "patrickkluivert@prive.nl"
        })
        persondata.Add(New Contact() With {
            .Firstname = "Robin",
            .Lastname = "van Persie",
            .Emailadress = "robinvanpersie@prive.nl"
        })
        persondata.Add(New Contact() With {
            .Firstname = "Arjen",
            .Lastname = "Robben",
            .Emailadress = "arjenrobben@prive.nl"
})
        Return persondata
    End Function

Class Contact:
Code:

        ' Storage of class Contact data
        Private _firstname As String
        Private _lastname As String
        Private _email As String

        Public Property Firstname() As String
            Get
                Return _firstname
            End Get
            Set(ByVal value As String)
                _firstname = value
            End Set
        End Property
        Public Property Lastname() As String
            Get
                Return _lastname
            End Get
            Set(ByVal value As String)
                _lastname = value
            End Set
        End Property
        Public Property Emailadress() As String
            Get
                Return _email
            End Get
            Set(ByVal value As String)
                _email = value
            End Set
        End Property

    End Class

End Class


Viewing all articles
Browse latest Browse all 15744

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>