Value of Type System.Collections.Generic.List(Of appname.formname.Object) cannot be converted to appname.formname.Object

Posted on 2011-10-29
Last Modified: 2012-05-12
Hi experts,

I'm hitting a wall here.  I am trying to serialize settings for use in network communications and I want to send the settings in an
xml format.  I use structures which does work for some simpler structures that I have but I am confused as to why it is telling me
that I cannot convert a Generic List Of a structure.  How can I get around this problem using the structures I have laid out? It is
erring out at design time on the line oTmpArea.BeaconList.Add(oTmpBeacons) with the error:
"Value of Type System.Collections.Generic.List(Of appname.formname.Object) cannot be converted to appname.formname.Object"

Thanks in advance!

#Region "Structures"

    <XmlRoot("RootMaps")> _
    Public Structure RootMaps
        <XmlElement("RootMaps")> Public RootMaps As List(Of RootMap)
    End Structure

    <XmlRoot("RootMap")> _
    Public Structure RootMap
        <XmlElement("Image")> Public Image As Byte()
        <XmlElement("UniqueID")> Public UniqueID As String
        <XmlElement("Sections")> Public Sections As List(Of Area)

    End Structure

    <XmlRoot("Area")> _
    Public Structure Area
        <XmlElement("Beacons")> Public BeaconsList As List(Of Beacon)
        <XmlElement("Height")> Public Height As Integer
        <XmlElement("SectionKey")> Public SectionKey As String
        <XmlElement("SectionName")> Public SectionName As String
        <XmlElement("TopLeftX")> Public TopLeftX As Integer
        <XmlElement("TopLeftY")> Public TopLeftY As Integer
        <XmlElement("Width")> Public Width As Integer
    End Structure

    <XmlRoot("Beacon")> _
    Public Structure Beacon
        <XmlElement("BTAddress")> Public BTAddress As String
    End Structure

#End Region

  Public Function CreateConfigPacket(ByVal ooBj As RootMaps) As RootMaps

        Dim oTmpRootMaps As New RootMaps
        Dim oTmpRootMap As RootMap
        Dim oTmpArea As Area
        Dim oTmpBeacons As List(Of Beacon)

        Dim oTmpBeacon As Beacon

        With g_oBTResponse.ConfigurationHolder

            For n = 0 To .RootMaps.Count - 1
                oTmpRootMap = New RootMap

                oTmpRootMap.Image = .RootMaps(n).Image
                oTmpRootMap.UniqueID = .RootMaps(n).UniqueId

                For nSec = 0 To .RootMaps(n).Sections.Count - 1
                    oTmpArea = New Area
                    oTmpBeacons = New List(Of Beacon)

                    For nBeacon = 0 To .RootMaps(n).Sections(nSec).Beacons.Count - 1
                        oTmpBeacon = New Beacon
                        oTmpBeacon.BTAddress = .RootMaps(n).Sections(nSec).Beacons(nBeacon).BTAddress
                    '**  Errors out on the line below

                    oTmpArea.Height = .RootMaps(n).Sections(nSec).Height
                    oTmpArea.SectionKey = .RootMaps(n).Sections(nSec).SectionKey
                    oTmpArea.SectionName = .RootMaps(n).Sections(nSec).SectionName
                    oTmpArea.TopLeftX = .RootMaps(n).Sections(nSec).TopLeftX
                    oTmpArea.TopLeftY = .RootMaps(n).Sections(nSec).TopLeftY
                    oTmpArea.Width = .RootMaps(n).Sections(nSec).Width

        End With

        Return ooBj
End Function

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Dim xmlSer As XmlSerializer
        'Dim memStm As MemoryStream = New MemoryStream()
        Dim oObj As New RootMaps
        oObj = CreateConfigPacket(oObj)
    End Sub

Open in new window

Question by:sharizod
    LVL 83

    Expert Comment

    The error is because you are tring to add a list where it is expecting a single beacon.

    Either change



    oTmpArea.BeaconList.AddRange(oTmpBeacons.ToArray()) 'Untested

    or use a loop to add all items from oTmpBeacons to the list.
    LVL 40

    Accepted Solution

    First of all, your choice of Structure is a bad choice. Because of the way they are handled in memory, structures should be small objects. Microsoft suggest no more than 16 bytes.

    Look at the structures in the framework (Size, Location, Rectangle). They are all small things that have but a few Integer values. I don't think you will find one structure that has a String in it, even less a collection.

    Then, for your program. A List is not an appname.formname.Object. Trying to do that type of conversion is akind to telling the system. to convert a collection of String objects into a unique String.

    BeaconList.Add is expecting a Beacon (unique object), you are adding a List (oTmpBeacons).
    LVL 1

    Author Comment

    Thanks for the comments thus far.  So then what would be a better or a recommended way to have an object hierarchy like this?  With a class instead of a structure?

    LVL 40

    Expert Comment

    by:Jacques Bourgeois (James Burger)
    Whenever you need to record a set of values that is greater than 16 bytes, a class is the recommended way.

    It will also probably improve the performance of your application, because a structure often has to me boxed and unboxed when you move it around, and this brings a penalty to performance. Lookup "boxing" in the documentation if you are interesting to know more.
    LVL 1

    Author Closing Comment

    Thanks!  I was able to get it working with a set of classes!

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now