Link to home
Start Free TrialLog in
Avatar of sharizod
sharizodFlag for Canada

asked on

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
                        oTmpBeacons.Add(oTmpBeacon)
                    Next
       
                    '**  Errors out on the line below
                    oTmpArea.BeaconsList.Add(oTmpBeacons)

                    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
                    oTmpRootMap.Sections.Add(oTmpArea)
                Next
                ooBj.RootMaps.Add(oTmpRootMap)
            Next

        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

Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Either change

oTmpArea.BeaconList.Add(oTmpBeacons)

to

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

or use a loop to add all items from oTmpBeacons to the list.
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sharizod

ASKER

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?

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.
Thanks!  I was able to get it working with a set of classes!