473,386 Members | 1,674 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,386 software developers and data experts.

Derived classes from interface don't cast

I am working on a project that I would like to have some extensibility for
later. So with this I am using interfaces to pass objects to derived classes.
However I am running into a situation where two seperate classes won't cast
each other even though they implement the same interface. The classes are
identical yet won't convert. Here is (briefly) what I am talking about:

public interface IReturnObject
{
Guid ID { get; set; }
string Status { get; set; }
string Message { get; set; }
}

public class Result : IReturnObject
{
//implements code here
}

public class Response : IReturnObject
{
//implements code here
}
NOTE: The above two classes are in different namespaces such that Result is
in System.Application1.Result and Response is in System.Application.Response.

So when I use this in a client app and try to cast these two classes against
each other it compiles but at runtime I get "Specified cast is not valid"
with a System.ArguementException.

Any help would be greatly appreciated.
Jul 21 '05 #1
4 3713
<"=?Utf-8?B?Q2xpbnQgSGlsbA==?=" <Clint
Hi**@discussions.microsoft.com>> wrote:
I am working on a project that I would like to have some extensibility for
later. So with this I am using interfaces to pass objects to derived classes.
However I am running into a situation where two seperate classes won't cast
each other even though they implement the same interface. The classes are
identical yet won't convert. Here is (briefly) what I am talking about:

public interface IReturnObject
{
Guid ID { get; set; }
string Status { get; set; }
string Message { get; set; }
}

public class Result : IReturnObject
{
//implements code here
}

public class Response : IReturnObject
{
//implements code here
}
NOTE: The above two classes are in different namespaces such that Result is
in System.Application1.Result and Response is in System.Application.Response.

So when I use this in a client app and try to cast these two classes against
each other it compiles but at runtime I get "Specified cast is not valid"
with a System.ArguementException.


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Note that you *won't* be able to cast an instance of Result to Response
or vice versa - but you should be able to cast each of them to
IReturnObject.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
I think you got the interface idea wrong. Many classes do implement, for
example, the IEnumerable interface; arrays do so, ArrayList, but also
HashTable and many other classes. Still you can't cast a HashTable to an
array or vice versa - they are unrelated classes and the only way to
"convert" them is to copy all the data item by item. The only thing you can
do is cast each of them to the IEnumerable interface. The idea is to write
code that only accesses classes through interfaces, so you can easily change
the implementation, as in:

void Output (IEnumerable lst)
{ foreach (object x in lst) Console.Writeline(x); }

This function will work for arrays, arraylists, hashtables...

Hope this helps,

Niki

"Clint Hill" <Clint Hi**@discussions.microsoft.com> wrote in
news:E2**********************************@microsof t.com...
I am working on a project that I would like to have some extensibility for
later. So with this I am using interfaces to pass objects to derived classes. However I am running into a situation where two seperate classes won't cast each other even though they implement the same interface. The classes are
identical yet won't convert. Here is (briefly) what I am talking about:

public interface IReturnObject
{
Guid ID { get; set; }
string Status { get; set; }
string Message { get; set; }
}

public class Result : IReturnObject
{
//implements code here
}

public class Response : IReturnObject
{
//implements code here
}
NOTE: The above two classes are in different namespaces such that Result is in System.Application1.Result and Response is in System.Application.Response.
So when I use this in a client app and try to cast these two classes against each other it compiles but at runtime I get "Specified cast is not valid"
with a System.ArguementException.

Any help would be greatly appreciated.

Jul 21 '05 #3
Clint Hill <Cl*******@discussions.microsoft.com> wrote:

<snip>

Right. So you really are trying to cast an object to a class which it
isn't. You can't do that. You should use an interface which supports
everything you need.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4
Thank you. I believe I understand it now.

"Niki Estner" wrote:
I think you got the interface idea wrong. Many classes do implement, for
example, the IEnumerable interface; arrays do so, ArrayList, but also
HashTable and many other classes. Still you can't cast a HashTable to an
array or vice versa - they are unrelated classes and the only way to
"convert" them is to copy all the data item by item. The only thing you can
do is cast each of them to the IEnumerable interface. The idea is to write
code that only accesses classes through interfaces, so you can easily change
the implementation, as in:

void Output (IEnumerable lst)
{ foreach (object x in lst) Console.Writeline(x); }

This function will work for arrays, arraylists, hashtables...

Hope this helps,

Niki

"Clint Hill" <Clint Hi**@discussions.microsoft.com> wrote in
news:E2**********************************@microsof t.com...
I am working on a project that I would like to have some extensibility for
later. So with this I am using interfaces to pass objects to derived

classes.
However I am running into a situation where two seperate classes won't

cast
each other even though they implement the same interface. The classes are
identical yet won't convert. Here is (briefly) what I am talking about:

public interface IReturnObject
{
Guid ID { get; set; }
string Status { get; set; }
string Message { get; set; }
}

public class Result : IReturnObject
{
//implements code here
}

public class Response : IReturnObject
{
//implements code here
}
NOTE: The above two classes are in different namespaces such that Result

is
in System.Application1.Result and Response is in

System.Application.Response.

So when I use this in a client app and try to cast these two classes

against
each other it compiles but at runtime I get "Specified cast is not valid"
with a System.ArguementException.

Any help would be greatly appreciated.


Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: James Brown | last post by:
I have two classes, a base class, and a class base { public: base(); virtual void foo() = 0; }; class derived : public base
5
by: StuartM | last post by:
Can anybody point me towards a sample of serializing/deserializing specific classes that are derived from a generic class? Is it necessary to use a switch statement or can it be done in a more...
4
by: Clint Hill | last post by:
I am working on a project that I would like to have some extensibility for later. So with this I am using interfaces to pass objects to derived classes. However I am running into a situation where...
8
by: Manuel | last post by:
Hi! If I've a vector filled with abstract classes, can I push in it the derived classes too? Even if derived classes have new methods? I've done some experiments, and it seem I can push the...
24
by: AtariPete | last post by:
Hey All, I have a C# question for you regarding up casting (base to derived). I was wondering about the most elegant way (readable, less code) to cast from a base type to its derived type....
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
5
by: gw7rib | last post by:
I'm writing a program which has "notes" - these can appear on the screen as windows with text in. It is possible to create an "index note" - at present, this will contain a list of the titles (or...
3
by: Filimon Roukoutakis | last post by:
Dear all, assuming that through a mechanism, for example reflexion, the Derived** is known explicitly. Would it be legal (and "moral") to do this conversion by a cast (probably reinterpret would...
6
by: Christopher | last post by:
I've seen various ways of doing this, but what I want is to be able to take a base class pointer and know which derived class to cast to. For example I am going to make a base light class, that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.