473,378 Members | 1,493 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,378 software developers and data experts.

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.WriteLine( "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
Aug 16 '08 #1
3 1149
On Fri, 15 Aug 2008 22:12:26 -0700, Russell Mangel <ru*****@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
Aug 16 '08 #2
>
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
Aug 16 '08 #3
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
Aug 16 '08 #4

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...
26
by: Marius Horak | last post by:
As in subject. Thanks MH
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: John Salerno | last post by:
I understand how they work (basically), but I think maybe the examples I'm reading are too elementary to really show their value. Here's one from Programming C#: #region Using directives ...
3
by: Ratan | last post by:
Hi All, We have a .NET library having .NET remoting based programming interface developed using VC++.NET (VS 2005 Beta2). We want to provide C interface for a legacy application. Is it possible?...
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...
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...
5
by: Russell Mangel | last post by:
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.