[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 397
  • Last Modified:

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

0
sharizod
Asked:
sharizod
  • 2
  • 2
1 Solution
 
CodeCruiserCommented:
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.
0
 
Jacques Bourgeois (James Burger)Commented:
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).
0
 
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?

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now