472,958 Members | 2,128 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 1178
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 Lwys "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.
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...

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.