By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,699 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,699 IT Pros & Developers. It's quick & easy.

Using interfaces to convert data between classes - Web Services

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

John Cortright wrote:
[snip]
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 .... End Class

Public Class Orange
Implements IFruit
.... 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?


No, you are going to have to explicitly specify in some way the
semantics of how an Orange is created from an Apple. Consider a
different example of the same thing: Hashtable and Queue both implement
ICollection, but you wouldn't expect there to be a straightforward way
of converting one to the other without going into some detail about how
to do it, would you?

As you mention, writing a constructor for Orange that takes an IFruit
(not specifically an Apple, mind) is the way to go here.

I imagine that in your actual scenario the conversion makes more sense
than converting an Apple to an Orange, btw! :)

--
Larry Lard
Replies to group please

Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.