473,326 Members | 2,173 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,326 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 3708
<"=?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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.