PC Review


Reply
Thread Tools Rate Thread

Derived classes from interface don't cast

 
 
=?Utf-8?B?Q2xpbnQgSGlsbA==?=
Guest
Posts: n/a
 
      6th Aug 2004
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.
 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      6th Aug 2004
<"=?Utf-8?B?Q2xpbnQgSGlsbA==?=" <Clint
(E-Mail Removed)>> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Niki Estner
Guest
Posts: n/a
 
      7th Aug 2004
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 (E-Mail Removed)> wrote in
news:E2497A2F-5EC3-44A5-9ECC-(E-Mail Removed)...
> 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.



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      7th Aug 2004
Clint Hill <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
=?Utf-8?B?Q2xpbnQgSGlsbA==?=
Guest
Posts: n/a
 
      7th Aug 2004
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 (E-Mail Removed)> wrote in
> news:E2497A2F-5EC3-44A5-9ECC-(E-Mail Removed)...
> > 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.

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Invalid cast from base to derived INeedADip Microsoft C# .NET 10 18th Sep 2007 03:03 PM
Cast from parent(base) to son(derived) class? =?ISO-8859-1?Q?=22Andr=E9s_G=2E_Aragoneses_=5B_kno Microsoft C# .NET 11 19th Dec 2006 09:13 PM
Focuse all derived classes to implement a interface specified by the base class Jinsong Liu Microsoft C# .NET 2 7th Jul 2006 08:53 AM
Newbie on Inheritance, Base classes and derived classes aaj Microsoft C# .NET 2 21st Jun 2005 03:52 PM
Cast from derived to base fails after Activator.CreateInstance() call Rob Richardson Microsoft C# .NET 3 29th Dec 2004 07:39 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:33 PM.