473,411 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 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 1149

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 a static IP to the my network adapter.
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 conform to any standard style of coding I have seen....
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 I convert those Classes to Web Services? is it...
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 "client", system 1) running on .NET 2.0/WinXP, calling...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.