423,851 Members | 1,356 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,851 IT Pros & Developers. It's quick & easy.

Why/How two objects require Widening/Narrowing conversions

!NoItAll
100+
P: 293
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...
Expand|Select|Wrap|Line Numbers
  1. Public Class PanelInfo
  2.     <XmlElement("sourceowner")> Public Property SourceOwner As New String(String.Empty)
  3.     <XmlElement("locationname")> Public Property LocationName As New String(String.Empty)
  4.     <XmlElement("description")> Public Property Description As New String(String.Empty)
  5.     <XmlElement("secondarydescription")> Public Property SecondaryDescription As New String(String.Empty)
  6.     <XmlElement("misc")> Public Property Misc As New String(String.Empty)
  7.     <XmlElement("geolocation")> Public Property Geolocation As New GeolocationInfo_Type
  8.     <XmlElement("copyright")> Public Property Copyright As New String(String.Empty)
  9.  
  10.     Private _CameraType As New String("unknown")
  11.     <XmlElement("cameratype")> Public Property CameraType As String
  12.         Get
  13.             Return _CameraType
  14.         End Get
  15.         Set(value As String)
  16.             Select Case value.ToLower
  17.                 Case "ptz"
  18.                     _CameraType = value
  19.                 Case "fixed"
  20.                     _CameraType = value
  21.                 Case "unknown"
  22.                     _CameraType = value
  23.                 Case Else
  24.                     _CameraType = "unknown"
  25.                     Throw New Exception("Only 'ptz', 'fixed' or 'unknown' are valid camera types")
  26.             End Select
  27.         End Set
  28.     End Property
  29.     Public Function XML() As String
  30.         Using ser As New Serialization.Serializer
  31.             Return ser.Obj2XML(Me, GetType(PanelInfo))
  32.         End Using
  33.     End Function
  34.  
  35.     Public Sub LoadXML(XML As String)
  36.         Using ser As New Serialization.Serializer
  37.             Dim This As PanelInfo = ser.XML2Obj(XML, GetType(PanelInfo))
  38.             Me.SourceOwner = This.SourceOwner
  39.             Me.LocationName = This.LocationName
  40.             Me.Description = This.Description
  41.             Me.SecondaryDescription = This.SecondaryDescription
  42.             Me.Misc = This.Misc
  43.             Me.Geolocation = This.Geolocation
  44.             Me.Copyright = This.Copyright
  45.             Me.CameraType = This.CameraType
  46.         End Using
  47.     End Sub
  48.  
  49. End Class
  50.  
  51. Public Class GeolocationInfo_Type
  52.     <XmlAttribute("latitude")> Public Property Latitude As New String(String.Empty)
  53.     <XmlAttribute("longitude")> Public Property Longitude As New String(String.Empty)
  54.     <XmlAttribute("elevation")> Public Property Elevation As New String(String.Empty)
  55. End Class
  56.  
Now the non-serializable one (you can see it does not have the XML or LoadXML methods implemented - nor the serialization decorations)

Expand|Select|Wrap|Line Numbers
  1. Public Class PanelInfo
  2.     Public Property SourceOwner As New String(String.Empty)
  3.     Public Property LocationName As New String(String.Empty)
  4.     Public Property Description As New String(String.Empty)
  5.     Public Property SecondaryDescription As New String(String.Empty)
  6.     Public Property Misc As New String(String.Empty)
  7.     Public Property Geolocation As New Geolocation_Type
  8.     Public Property Copyright As New String(String.Empty)
  9.  
  10.     Private _CameraType As New String("unknown")
  11.     Public Property CameraType As String
  12.         Get
  13.             Return _CameraType
  14.         End Get
  15.         Set(value As String)
  16.             Select Case value.ToLower
  17.                 Case "ptz"
  18.                     _CameraType = value
  19.                 Case "fixed"
  20.                     _CameraType = value
  21.                 Case "unknown"
  22.                     _CameraType = value
  23.                 Case Else
  24.                     _CameraType = "unknown"
  25.                     Throw New Exception("Only 'ptz', 'fixed' or 'unknown' are valid camera types")
  26.             End Select
  27.         End Set
  28.     End Property
  29.  
  30.     Public Function XML() As String
  31.         Throw New NotImplementedException("Not Implemented - non serializable class")
  32.     End Function
  33.  
  34.     Public Sub LoadXML(XML As String)
  35.         Throw New NotImplementedException("Not Implemented - non serializable class")
  36.     End Sub
  37.  
  38. End Class
  39.  
  40. Public Class Geolocation_Type
  41.     Public Property Latitude As New String(String.Empty)
  42.     Public Property Longitude As New String(String.Empty)
  43.     Public Property Elevation As New String(String.Empty)
  44. End Class
  45.  
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 NotImplementedException - and I'm not clear on what I need to code inside this conversion...
Any help would be greatly appreciated.
Oct 9 '18 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.