472,352 Members | 1,434 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 software developers and data experts.

Using interfaces to convert data between classes - Web Services

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
1 1111

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Shryeash Patel | last post by:
I wanted to know how I can switch between the "Obtain an IP address from a DHCP Server" and "Specify an IP Address". In addition, how I can assign...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not...
3
by: Zean Smith | last post by:
I wrote an ASPX web application and a C# Classes which compiled as a seperated DLLs. In my ASPX web app. I import that DLL as Reference. How do...
3
by: Michael Hoehne | last post by:
Hi, I'm currently facing a problem with a mixed environment using .NET 1.1 and ..NET 2.0 web services. We have a client application (the...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.