473,385 Members | 1,869 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,385 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 1193
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Pierre Barbier de Reuille | last post by:
Ok, I first want to stress that I looked through the newsgroups archives (on Google) for an answer to my question, but I didn't find it. It's about the interface of the set classes as defined in...
5
by: C. Barnes | last post by:
Hi, I'm in the process of writing a Python linear algebra module. The current targeted interface is: http://oregonstate.edu/~barnesc/temp/linalg/ The interface was originally based on...
5
by: Patrick Kristiansen | last post by:
Hi group! I've been reading Juval Löwys "Programming .NET Components", and I think it is a very good book, giving nice guidelines on how to really exhaust the possibilities of .NET in general. ...
6
by: Ricky W. Hunt | last post by:
It's dawning on my a lot of my problems with VB.NET is I'm still approaching it in the same way I've programmed since the late 70's. I've always been very structured, flow-charted everything, used...
7
by: tshad | last post by:
I am trying to understand why I would use interfaces. In the following example for IPrinciple, I have the following code: ************************************************************ using...
20
by: Luc Kumps | last post by:
(Sorry about the previous post, it got transmitted before it was complete) We try to separate implementation and interface defintions, but we run into a problem. I hope the guru's can solve this,...
0
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
8
by: rn5a | last post by:
Suppose I have the following class code: Imports System Imports System.Data Imports System.Data.SqlClient Public Class DBSettings Private sqlCmd As SqlCommand Private sqlConn As...
3
by: =?ISO-8859-1?Q?Arne_Vajh=F8j?= | last post by:
Russell Mangel wrote: You have a MsgFile object and it does not implement INoteItem, so you can not cast to it.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...

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.