interface type casting help!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Brown

    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:

    AbstractViewCon tent -> Implements IViewContent interface
    AbstractDatabas eView
    TableEditorView
    ProcedureEditor View
    ViewEditorView

    I have an implemented method call in a class ContentBinding,

    IViewContent CreateViewConte nt(object obj)

    that return TableEditorView , ProcedureEditor View 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(Ab stractDatabaseO bject obj) {
    ContentBinding binding = new ContentBinding( );
    AbstractDatabas eView content = (AbstractDataba seView)
    binding.CreateV iewContent(obj) ;
    content.Load(ob j);
    }

    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 .


  • Jeff Louie

    #2
    Re: interface type casting help!

    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.CreateV iewContent(obj) ;
    if (content is AbstractDataVie w) { // valid type and not null
    ... cast here
    }
    else {
    .. alert here, or breakpoint here in debugger
    }

    Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


    Regards,
    Jeff[color=blue]
    >This compiles just fine, however, during runtime, I get an invalid type[/color]
    cast exception on the second line!<


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!

    Comment

    • Jon Skeet

      #3
      Re: interface type casting help!

      Richard Brown <rbrown@easylif t.org> wrote:[color=blue]
      > I'm running into a big problem, and it's probably just my lack of C#
      > knowledge, but can someone give me a clue?[/color]

      <snip>

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

      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      Working...