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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Russell Mangel

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

    Here is something much closer to what I was trying to do.

    Thanks to Arne, and Peter and all who helped.
    Sorry I didn't explain my question better.

    Russell Mangel
    Las Vegas, NV

    // Begin Code
    using System;

    public class Client
    {
    public void Test( )
    {
    using ( IMsgFile m = new MsgFile( "file" ) )
    {
    // IMsgFile has GetAttachments( ) and GetRecipients()
    Console.WriteLi ne( m.MessageClass );

    INoteItem n = m as INoteItem;
    if ( n == null )
    {
    Console.WriteLi ne( "Error casting to INoteItem." );
    }
    else
    {
    // NoteItem does not have GetAttachments( ) or GetRecipients()
    Console.WriteLi ne( n.Color );
    }

    IPostItem p = m as IPostItem;
    if ( p == null )
    {
    Console.WriteLi ne( "Error casting to IPostItem." );
    }
    else
    {
    // PostItem has only GetAttachments( );
    Console.WriteLi ne( p.PostItemSize );
    }

    // Cast back to IMsgFile
    IMsgFile m2 = n as IMsgFile;
    if ( m2 == null )
    {
    Console.WriteLi ne( "Error casting to back IMsgFile." );
    }
    else
    {
    Console.WriteLi ne( m2.MessageClass );
    }
    }
    }
    }
    public interface IMsgFile : IDisposable
    {
    String MessageClass
    {
    get;
    }
    IAttachment[] GetAttachments( );
    IRecipient[] GetRecipients( );
    }
    public class MsgFile : IMsgFile, INoteItem, IPostItem
    {
    public MsgFile( String file )
    {
    }

    #region IMsgFile Members

    string IMsgFile.Messag eClass
    {
    get
    {
    return "IPM.Note";
    }
    }

    IAttachment[] IMsgFile.GetAtt achments( )
    {
    throw new NotImplementedE xception( );
    }

    IRecipient[] IMsgFile.GetRec ipients( )
    {
    throw new NotImplementedE xception( );
    }

    #endregion

    #region IDisposable Members

    void IDisposable.Dis pose( )
    {
    Console.WriteLi ne( "Dispose Called." );
    }

    #endregion

    #region INoteItem Members

    int INoteItem.Color
    {
    get
    {
    return 6;
    }
    }

    #endregion

    #region IPostItem Members

    int IPostItem.PostI temSize
    {
    get
    {
    return 10998;
    }
    }

    IAttachment[] IPostItem.GetAt tachments( )
    {
    throw new NotImplementedE xception( );
    }

    #endregion
    }

    public interface INoteItem
    {
    Int32 Color
    {
    get;
    }
    }
    public interface IPostItem
    {
    Int32 PostItemSize
    {
    get;
    }
    IAttachment[] GetAttachments( );
    }

    public interface IAttachment
    {
    String AttachmentName
    {
    get;
    }
    }
    public interface IRecipient
    {
    String RecipientName
    {
    get;
    }
    }
    // End Code


  • Peter Duniho

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

    On Sat, 16 Aug 2008 02:50:32 -0700, Russell Mangel <russell@tymer. net>
    wrote:
    Here is something much closer to what I was trying to do.
    >
    Thanks to Arne, and Peter and all who helped.
    Sorry I didn't explain my question better.
    Sorry to say, I'm not sure your more recent post is better. I don't see
    any material difference. You use a different MsgFile constructor, but
    otherwise it has the same problem. You allocate a MsgFile instance, and
    that class simply does not implement the INoteItem interface. As long as
    you are only creating a MsgFile instance, this will never work, no matter
    how you create it.

    Pete

    Comment

    • Russell Mangel

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

      >
      Sorry to say, I'm not sure your more recent post is better. I don't see
      any material difference. You use a different MsgFile constructor, but
      otherwise it has the same problem. You allocate a MsgFile instance, and
      that class simply does not implement the INoteItem interface. As long as
      you are only creating a MsgFile instance, this will never work, no matter
      how you create it.
      >
      Pete
      I don't understand are you looking at the right source code?

      The new file shows MsgFile implementing the 3 Interfaces + IDisposable
      like this. The MsgFile has the Implementation for all interfaces.
      Or am I crazy?

      // Here is a partial listing of class MsgFile
      public class MsgFile : IMsgFile, INoteItem, IPostItem
      {
      public MsgFile( String file ) { }

      int INoteItem.Color { get { return 6; }}

      int IPostItem.PostI temSize { get { return 10998; }}
      }

      Russell Mangel
      Las Vegas, NV


      Comment

      • Peter Duniho

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

        On Sat, 16 Aug 2008 22:03:53 -0700, Russell Mangel <russell@tymer. net>
        wrote:
        I don't understand are you looking at the right source code?
        >
        The new file shows MsgFile implementing the 3 Interfaces + IDisposable
        like this. The MsgFile has the Implementation for all interfaces.
        Sorry, I didn't notice that change.

        But with the change, now I don't know what the question is. Is there
        something about the new code that doesn't do what you want?

        Pete

        Comment

        • Russell Mangel

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


          "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
          news:op.uf0mft1 i8jd0ej@petes-computer.local. ..
          On Sat, 16 Aug 2008 22:03:53 -0700, Russell Mangel <russell@tymer. net>
          wrote:
          >
          >I don't understand are you looking at the right source code?
          >>
          >The new file shows MsgFile implementing the 3 Interfaces + IDisposable
          >like this. The MsgFile has the Implementation for all interfaces.
          >
          Sorry, I didn't notice that change.
          >
          But with the change, now I don't know what the question is. Is there
          something about the new code that doesn't do what you want?
          >
          Pete
          No problems anymore.
          I just posted the latest working code for other users that
          might be reading the posts. It's always nice to see the final
          result after several people have made suggestions and
          changes.

          Thanks for your help.

          Russell Mangel
          Las Vegas, NV


          Comment

          • Peter Duniho

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

            On Sat, 16 Aug 2008 23:13:43 -0700, Russell Mangel <russell@tymer. net>
            wrote:
            "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
            news:op.uf0mft1 i8jd0ej@petes-computer.local. ..
            >[..]
            >But with the change, now I don't know what the question is. Is there
            >something about the new code that doesn't do what you want?
            >
            No problems anymore.
            I just posted the latest working code for other users that
            might be reading the posts. It's always nice to see the final
            result after several people have made suggestions and
            changes.
            Ahh, okay. Sorry for confusing things. :)

            Comment

            Working...