473,387 Members | 3,821 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,387 software developers and data experts.

interface type casting help!

I'm running into a big problem, and it's probably just my lack of C#
knowledge, but can someone give me a clue?

I'm using the folling object heirarchy:

AbstractViewContent -> Implements IViewContent interface
AbstractDatabaseView
TableEditorView
ProcedureEditorView
ViewEditorView

I have an implemented method call in a class ContentBinding,

IViewContent CreateViewContent(object obj)

that return TableEditorView, ProcedureEditorView or ViewEditorView depending
on the type of object.
(the IViewContent return type *cannot* be changed because it is part of a
library, I can only override the member method)

Now, in my other method,

void ObjectObject(AbstractDatabaseObject obj) {
ContentBinding binding = new ContentBinding();
AbstractDatabaseView content = (AbstractDatabaseView)
binding.CreateViewContent(obj);
content.Load(obj);
}

This compiles just fine, however, during runtime, I get an invalid type cast
exception on the second line!
Is it not possible to 'cast back down' from an implemented interface,
through the inheritance tree to a specifc child object?
I have traced the code, and, for the object I am trying to 'open', the
create view is TableEditorView.
Nov 15 '05 #1
2 2787
Richard... I am afraid I really don't your understand the class
hiearchy,
_but_ if I needed to debug this I would add;

IViewContent content = binding.CreateViewContent(obj);
if (content is AbstractDataView) { // valid type and not null
... cast here
}
else {
.. alert here, or breakpoint here in debugger
}

http://www.geocities.com/jeff_louie/OOP/oop6.htm

Regards,
Jeff
This compiles just fine, however, during runtime, I get an invalid type

cast exception on the second line!<
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #2
Richard Brown <rb****@easylift.org> wrote:
I'm running into a big problem, and it's probably just my lack of C#
knowledge, but can someone give me a clue?


<snip>

What you're doing should be fine, if CreateViewContent really *does*
return an AbstractDataView instance reference. However, if this is all
being done in a plug-in type scenario, you *may* be running into the
problem described at:

http://www.pobox.com/~skeet/csharp/plugin.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3

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

Similar topics

3
by: Jeff Molby | last post by:
Ok, I've googled long and hard, but I can't find anything relevant on this one. I am now at the mercy of your good nature. <g> I have 3 projects in my solution: One app and 2 libraries. I am...
20
by: Simon Harvey | last post by:
Festive greetings fellow programmers! I've been programming now for about 4, maybe 5 years now. 4 of those years were at university so and I havent had much work experience of making real world...
12
by: Steve W. | last post by:
I just read the section (and did the exercise) in the C# Step by Step book that covers Explict Interface Implementation (where you specify in the method implementation the specific interface that...
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
3
by: thomson | last post by:
Hi all, Can any one tell me what happens when an object is casted to an interface, somthing bit deeper, the case is i do have an interface Ifunction which has got one method display(), and i...
10
by: Bob | last post by:
This has been bugging me for a while now. GetType isn't availble for variables decalred as interface types, I have to DirectCast(somevariable, Object). In example: Sub SomeSub(ByVal...
15
by: mr.peteryu | last post by:
Hi, Can someone explain the idea behind casting to an interface? For example: -> I have an IInterface that contains a Read() method. -> I have an object "obj" that implements IInterface. ...
2
by: gunters | last post by:
Can please someone enlight me, I don't understand this: It is possible in C# to "reach" inside a boxed value type through an interface like in the following code: interface IPerson { string...
9
by: Sean Chambers | last post by:
Not sure if this is the correct way to go about this, but it seems correct. I'm trying to eliminate dependencies through interfaces and want to minimize casting as much as possible, as a result I...
8
by: Lamefif | last post by:
// C3DRect supports IDraw and IShapeEdit. class C3DRect : public IDraw, public IShapeEdit { public: C3DRect(); virtual ~C3DRect(); // IDraw virtual void Draw(); // IShapeEdit virtual void...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.