Re: Interface based programming question : Why doesn't this codework?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

    Re: Interface based programming question : Why doesn't this codework?

    Russell Mangel wrote:
    Can anyone tell me *why* I can't cast to INoteItem from IMsgFile?
    All interfaces are Explicit. INoteItem derives from MsgFile, not IMsgItem.
    Why doesn't this code work? What is the rule I don't understand.
    Please don't change the code, just answer my question.
    using ( MsgFile m = new MsgFile( ) )
    {
    INoteItem n = m as INoteItem;
    >
    if ( n == null )
    {
    Console.WriteLi ne( "Why can't I cast to INoteItem." );
    }
    public interface INoteItem
    {
    internal class NoteItem: MsgFile, INoteItem
    {
    public interface IMsgFile: IDisposable
    {
    public class MsgFile : IDisposable, IMsgFile
    {
    You have a MsgFile object and it does not implement INoteItem, so
    you can not cast to it.

    If you has has a NoteItem object, then you could have done it.

    Arne
  • Peter Duniho

    #2
    Re: Interface based programming question : Why doesn't this codework?

    On Fri, 15 Aug 2008 22:12:26 -0700, Russell Mangel <russell@tymer. net>
    wrote:
    Okay, maybe you can fix this for me. Let me give you some rules.
    1. NoteItem must inherit from Base Class MsgFile.
    2. NoteItem class must be hidden from client.
    What code is the "client" here? Did you post any of the client code yet?
    Or is it completely independent of the code you're dealing with right now?
    3. MsgFile class has IAttachment[] array.
    4. INoteItem or NoteItem does not have IAttachment[] array.
    That's not true. You already said that NoteItem inherits MsgFile, so if
    MsgFile has an IAttachment[], so does NoteItem. You don't appear to be
    posted complete declarations of your classes and interfaces, so I can't
    say whether that's also true of INoteItem.
    5. Attachment class is hidden from client.
    I'm not sure how that's relevant.

    Anyway, as far as your original code goes, Arne already provided the only
    answer we could possibly offer at this point. You seem to want to be able
    to get an INoteItem reference, but to do that you have to start with an
    instance of a class that implements INoteItem. For example, NoteItem. In
    the code you posted, you allocate a MsgFile, not a NoteItem, and MsgFile
    doesn't implement INoteItem.

    There are a variety of design approaches that can be used in this sort of
    situation, but they all involve a common idea: allocating a NoteItem
    instance when what you've got is a NoteItem.

    Pete

    Comment

    • Russell Mangel

      #3
      Re: Interface based programming question : Why doesn't this code work?

      >
      You have a MsgFile object and it does not implement INoteItem, so
      you can not cast to it.
      That's what I needed. Thanks for your answer.

      See my post to myself for code that is much closer to what I was trying
      to do.

      Russell Mangel
      Las Vegas, NV


      Comment

      • Russell Mangel

        #4
        Re: Interface based programming question : Why doesn't this code work?

        Anyway, as far as your original code goes, Arne already provided the only
        answer we could possibly offer at this point.
        Exactly!
        You seem to want to be able to get an INoteItem reference, but to do that
        you have to start with an instance of a class that implements INoteItem.
        For example, NoteItem. In the code you posted, you allocate a MsgFile,
        not a NoteItem, and MsgFile doesn't implement INoteItem.
        I had it backwards.

        This is what happens to me when I am wearing my implementation hat, you
        forget about your architect hat.

        See my post to myself for code that is much closer to what I was trying
        to do.

        Thanks for your help.

        Russell Mangel
        Las Vegas, NV


        Comment

        Working...