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

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

Who is Participating?
Jacques Bourgeois (James Burger)Connect With a Mentor PresidentCommented:
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).
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.
sharizodAuthor Commented:
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?

Jacques Bourgeois (James Burger)PresidentCommented:
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.
sharizodAuthor Commented:
Thanks!  I was able to get it working with a set of classes!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.