Derived classes from interface don't cast

G

Guest

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.
 
J

Jon Skeet [C# MVP]

<"=?Utf-8?B?Q2xpbnQgSGlsbA==?=" <Clint
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.
 
N

Niki Estner

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
 
J

Jon Skeet [C# MVP]

<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.
 
G

Guest

Thank you. I believe I understand it now.

Niki Estner said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top