Why/How two objects require Widening/Narrowing conversions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • !NoItAll
    Contributor
    • May 2006
    • 297

    Why/How two objects require Widening/Narrowing conversions

    I'm a bit lost...
    I have two objects implemented as UserControls (compiled as separate DLLs). Both have implemented a specific property that is, in itself, an object. I need to pass this object between the two UserControls. Both of these internal properties are created separately and contain properties named the same and are typed the same. One, however is serializable, while the other is not. One contains two methods that the other does not. Otherwise each contain string properties that are, indeed, all the same.
    VB.Net will not let me assign one to the other either directly or through TryCast without either a widening or narrowing conversion. I admit to not understanding what I should actually do in this conversion, so I am asking for some guidance. Here are my two properties:
    First - the serializable one...
    Code:
    Public Class PanelInfo
        <XmlElement("sourceowner")> Public Property SourceOwner As New String(String.Empty)
        <XmlElement("locationname")> Public Property LocationName As New String(String.Empty)
        <XmlElement("description")> Public Property Description As New String(String.Empty)
        <XmlElement("secondarydescription")> Public Property SecondaryDescription As New String(String.Empty)
        <XmlElement("misc")> Public Property Misc As New String(String.Empty)
        <XmlElement("geolocation")> Public Property Geolocation As New GeolocationInfo_Type
        <XmlElement("copyright")> Public Property Copyright As New String(String.Empty)
    
        Private _CameraType As New String("unknown")
        <XmlElement("cameratype")> Public Property CameraType As String
            Get
                Return _CameraType
            End Get
            Set(value As String)
                Select Case value.ToLower
                    Case "ptz"
                        _CameraType = value
                    Case "fixed"
                        _CameraType = value
                    Case "unknown"
                        _CameraType = value
                    Case Else
                        _CameraType = "unknown"
                        Throw New Exception("Only 'ptz', 'fixed' or 'unknown' are valid camera types")
                End Select
            End Set
        End Property
        Public Function XML() As String
            Using ser As New Serialization.Serializer
                Return ser.Obj2XML(Me, GetType(PanelInfo))
            End Using
        End Function
    
        Public Sub LoadXML(XML As String)
            Using ser As New Serialization.Serializer
                Dim This As PanelInfo = ser.XML2Obj(XML, GetType(PanelInfo))
                Me.SourceOwner = This.SourceOwner
                Me.LocationName = This.LocationName
                Me.Description = This.Description
                Me.SecondaryDescription = This.SecondaryDescription
                Me.Misc = This.Misc
                Me.Geolocation = This.Geolocation
                Me.Copyright = This.Copyright
                Me.CameraType = This.CameraType
            End Using
        End Sub
    
    End Class
    
    Public Class GeolocationInfo_Type
        <XmlAttribute("latitude")> Public Property Latitude As New String(String.Empty)
        <XmlAttribute("longitude")> Public Property Longitude As New String(String.Empty)
        <XmlAttribute("elevation")> Public Property Elevation As New String(String.Empty)
    End Class
    Now the non-serializable one (you can see it does not have the XML or LoadXML methods implemented - nor the serialization decorations)

    Code:
    Public Class PanelInfo
        Public Property SourceOwner As New String(String.Empty)
        Public Property LocationName As New String(String.Empty)
        Public Property Description As New String(String.Empty)
        Public Property SecondaryDescription As New String(String.Empty)
        Public Property Misc As New String(String.Empty)
        Public Property Geolocation As New Geolocation_Type
        Public Property Copyright As New String(String.Empty)
    
        Private _CameraType As New String("unknown")
        Public Property CameraType As String
            Get
                Return _CameraType
            End Get
            Set(value As String)
                Select Case value.ToLower
                    Case "ptz"
                        _CameraType = value
                    Case "fixed"
                        _CameraType = value
                    Case "unknown"
                        _CameraType = value
                    Case Else
                        _CameraType = "unknown"
                        Throw New Exception("Only 'ptz', 'fixed' or 'unknown' are valid camera types")
                End Select
            End Set
        End Property
    
        Public Function XML() As String
            Throw New NotImplementedException("Not Implemented - non serializable class")
        End Function
    
        Public Sub LoadXML(XML As String)
            Throw New NotImplementedException("Not Implemented - non serializable class")
        End Sub
    
    End Class
    
    Public Class Geolocation_Type
        Public Property Latitude As New String(String.Empty)
        Public Property Longitude As New String(String.Empty)
        Public Property Elevation As New String(String.Empty)
    End Class
    I can go through each of the properties and assign the values, but I would rather just pass one property to the other - but the IDE says I need a Widening Conversion if I pass it directly, or a Narrowing Conversion if I use TryCast. The IDE is helpful in that it offers to create the conversion for me, but it is pre-filled with a NotImplementedE xception - and I'm not clear on what I need to code inside this conversion...
    Any help would be greatly appreciated.
Working...