Hello!
I've got an application that uses web services to traverse between various
tiers, and we're passing complex data types back and forth as both parameters
and return types.
Originally, I was serializing the complex types into a string, and then
passing that around, but we're investigating how much of a performance hit
(if any) that is causing us.
This is a rather simple question regarding interfaces, actually, and it all
boils down to this. How can I cast an object that inherits an interface into
another object that inherits the interface, without using an explicit
conversion with an adapter class or customized constructor? Let's say I have
the following defined...
Public Interface IFruit
Property Price() As Double
Property Color() As String
End Interface
Public Class Apple
Implements IFruit
Public Property Color() As String Implements IFruit.Color
Get
Return pColor
End Get
Set(ByVal Value As String)
pColor = Value
End Set
End Property
Public Property Price() As Double Implements IFruit.Price
Get
Return pPrice
End Get
Set(ByVal Value As Double)
pPrice = Value
End Set
End Property
Private pPrice As Double
Private pColor As String
End Class
Public Class Orange
Implements IFruit
Public Property Color() As String Implements IFruit.Color
Get
Return pColor
End Get
Set(ByVal Value As String)
pColor = Value
End Set
End Property
Public Property Price() As Double Implements IFruit.Price
Get
Return pPrice
End Get
Set(ByVal Value As Double)
pPrice = Value
End Set
End Property
Private pPrice As Double
Private pColor As String
End Class
I'd basically like to be able to do this:
Dim fred As New Apple
fred.Price = 0.89
fred.Color = "red"
Dim larry As Orange
larry = CType(fred, Orange)
I know that I can do:
larry = Ctype(fred, IFruit)
But it's really preferable to have it casted as an Orange. Obviously the
real life class and interface design is significantly more complicated, but
is there a way I can essentially do this, without writing a constructor for
Orange that accepts an "Apple" object or some kind of adapter?
Thanks for any input!
John