471,328 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,328 software developers and data experts.

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.WriteLine( m.MessageClass );

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

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

// Cast back to IMsgFile
IMsgFile m2 = n as IMsgFile;
if ( m2 == null )
{
Console.WriteLine( "Error casting to back IMsgFile." );
}
else
{
Console.WriteLine( 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.MessageClass
{
get
{
return "IPM.Note";
}
}

IAttachment[] IMsgFile.GetAttachments( )
{
throw new NotImplementedException( );
}

IRecipient[] IMsgFile.GetRecipients( )
{
throw new NotImplementedException( );
}

#endregion

#region IDisposable Members

void IDisposable.Dispose( )
{
Console.WriteLine( "Dispose Called." );
}

#endregion

#region INoteItem Members

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

#endregion

#region IPostItem Members

int IPostItem.PostItemSize
{
get
{
return 10998;
}
}

IAttachment[] IPostItem.GetAttachments( )
{
throw new NotImplementedException( );
}

#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
Aug 16 '08 #1
5 1111
On Sat, 16 Aug 2008 02:50:32 -0700, Russell Mangel <ru*****@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
Aug 16 '08 #2
>
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.PostItemSize { get { return 10998; }}
}

Russell Mangel
Las Vegas, NV
Aug 17 '08 #3
On Sat, 16 Aug 2008 22:03:53 -0700, Russell Mangel <ru*****@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
Aug 17 '08 #4

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Sat, 16 Aug 2008 22:03:53 -0700, Russell Mangel <ru*****@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
Aug 17 '08 #5
On Sat, 16 Aug 2008 23:13:43 -0700, Russell Mangel <ru*****@tymer.net>
wrote:
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@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. :)
Aug 17 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Pierre Barbier de Reuille | last post: by
5 posts views Thread by Patrick Kristiansen | last post: by
6 posts views Thread by Ricky W. Hunt | last post: by
7 posts views Thread by tshad | last post: by
reply views Thread by YellowFin Announcements | last post: by
8 posts views Thread by rn5a | last post: by
3 posts views Thread by =?ISO-8859-1?Q?Arne_Vajh=F8j?= | last post: by

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.