473,776 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Beginner interface question

I've been reading up on interfaces, one example I came across showed
that you can hide a method implemented from an interface from the class
which implements it. To use it, you must cast to the interface type
e.g.:

interface IFoo
{
void display();
}

class Blah : IFoo
{
void IFoo.display()
{
System.Console. WriteLine("hell o");
}
}

class MainApp
{
public static void Main()
{
Blah myBlah = new Blah();

myBlah.display( ); //error
((IFoo)myBlah). display(); //works
}
}

In what circumstances would you ever want to do this? The place I found
this didn't explain why this would ever be done.
Sorry if these questions are missing the obvious :).

Chris

Dec 12 '05
13 1766
MJ... If each object has special features then you might want to
implement a
persistance interface and each object would provide a custom behavior at
Save.
If most objects can be persisted using generic logic then you can rely
on generic
persistance. So most object persist using [Serializable] just fine, a
few do not
and need to also implement ISerializable such as Singleton classes,
providing a
custom behavior.

Regards,
Jeff
If I had a number of objects which were not derived from the same base

class. Each of these objects had a load and save method, which were
called on every instanced object when the user selected a save event.
Would it be best to create a load/save interface, and then use a method
such as above. Or would it be best to use a function which calls every
object in turn, decides what form the object is and then load/save. Or
even factoring all objects which need load and save functionality into
a common base class? (the load or save could be something like
drawToScreen etc).<

*** Sent via Developersdex http://www.developersdex.com ***
Dec 13 '05 #11
_Internal_ to a project, you may come to the day that you define the
contract
first for almost everything and then start building concrete classes. So
you
program to a contract, contract first, using Interfaces. Internal to a
project, you
will be amazed at how this helps. Say you need to add a new method to an
Interface. Just do it and the compiler flags every concrete class that
needs to be
updated.

Regards,
Jeff
I'm trying to get my head around when best to use interfaces etc.<


*** Sent via Developersdex http://www.developersdex.com ***
Dec 13 '05 #12
hello josh, first of all thanks for your reply, thank you all guys it
has been great to witness all these action and try to learn from it.
the following code works for me, i only have access to the properties
and methods that i want, but i am not quite sure that i doing this
right, i mean it could have been done without interfaces in the
following code and it would have worked, so appearntly i didnt quite get
it and as this newsgroups are my only source of learning so far i would
appreciate feedbacks and comments as much as possilbe, whatz the trick
??!!

interface IData
{
int ClientInfo { get;set;}
int OrderInfo { get;set;}
}
interface IClient
{
int ClientInfo { get;}
}
interface IAdministrator
{
int ClientInfo { get;set;}
int OrderInfo { get;set;}
}

class Data : IData
{
private int clientInfo = 0;
private int orderInfo = 0;
public Data()
{
this.clientInfo = clientInfo;
this.orderInfo= orderInfo;
}
public int ClientInfo
{
get { return this.clientInfo ; }
set { this.clientInfo = value; }
}
public int OrderInfo
{
get { return this.orderInfo; }
set { this.orderInfo = value; }
}
}

class Client : IClient
{
Data data = new Data();

public Client()
{
this.data = data;
}
public int ClientInfo
{
get { return data.ClientInfo ; }
}
}

namespace WebApplication1 2
{

class Administrator : IAdministrator
{
private IData data = null;
public Administrator()
{

this.data = data;
}
public int ClientInfo
{
get
{
return data.ClientInfo ;
}
set
{
this.data.Clien tInfo = value;
}
}
public int OrderInfo
{
get { return this.data.Order Info; }
set { this.data.Order Info = value; }
}
}
Sharing makes All the Difference

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Dec 14 '05 #13
Dave... You are right. Except for IData you could have solved this
problem
without an interface. The key concepts are
1) using containment by reference to adapt the readwrite IData interface
to a
limited IClient interface and
2) understanding that multiple objects can hold a reference to a single
object
without the need for reference counting.

Since Client and Administrator take IData as a parameter, you can pass a
different Data class in the future and the code will still work. In fact
you can
pass any class that implements IData to the constructors and it will
work in
the future.

If you don't find the ability to look at the public contract as:

interface IData
{
int ClientInfo { get;set;}
int OrderInfo { get;set;}
}
interface IClient
{
int ClientInfo { get;}
}
interface IAdministrator
{
int ClientInfo { get;set;}
int OrderInfo { get;set;}
}

helpful, then there is no need to abstract out all of the interfaces. At
least
until you write a gazillion concrete classes and then decide to modify a
single
contract. If you used interfaces, the compiler will find all of the
concrete
classes that need to be updated!

There are at least four times when interfaces or abstract base classes
are
useful:

Polymorphic behavior
Defering the final behavior to a more knowledgable class
Encapsulating or hiding the implementation
Marking a class

So try to write some code that uses these four idioms and you will see
the
light. If you need hints:

IDrawable
IComparable
IPlugIn
IRequiresSessio nState

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Dec 14 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2872
by: Art | last post by:
NEWBIE ALERT! Esteemed List Participants and Lurkers: (System: P-II 350, 192 meg, Win98 SE, Python 2.2.3, wxPythonWIN32-2.4.1.2-Py22.exe) I'm having a lot of fun getting started with Python ... it is the most elegant and graceful language I have ever used (Fortran, Cobol, Basic, many assemblers, Forth, C, VB, etc.). I don't have the resources or the
5
1639
by: Timothy Wu | last post by:
I'm writing a small utility that listens for socket connections, and also repond to user inputs via a text menu selection. In order to respond to both the user and the incoming connections I figure I need to use thread. I think I would use the main thread to handle the menu and spawn a thread which listens on a socket. Now my question is, how do I terminate the server thread if user wants
0
3724
by: Jari Kujansuu | last post by:
I am trying to learn to use jing through JARV interface and I have read JARV User's Guide (http://iso-relax.sourceforge.net/JARV/JARV.html). I have downloaded jingjarv-examples.zip from JARV User's Guide page and latest jing version 20030619 from jing home page. I can run jing successfully from command line like this (which I guess proves that schema should be correct) C:\Temp\APU> java -jar jing.jar schema.xsd valid.xml
44
4284
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there must be many know the answer here. thanks
2
1168
by: Paul | last post by:
Hi, I'm curious how others would deal with something I'm building. The app itself is a form of diary; it allows the organisation of diary entries and a number of other data types. Each diary entry is listed under its title, and double clicking brings up the full entry and options for editing. Each entry and associated option is stored in a diary object instance. I'm wondering how best to store the persistent data. One option is to a...
9
5519
by: hharry | last post by:
hello all, switching to c# from vb.net and had quick syntax question... in vb: Dim httpRequest as HttpWebRequest httpRequest = WebRequest.Create( _ "http://www.winisp.net/goodrich/default.htm")
6
3008
by: Qun Cao | last post by:
Hi Everyone, I am a beginner on cross language development. My problem at hand is to build a python interface for a C++ application built on top of a 3D game engine. The purpose of this python interface is providing a convenient scripting toolkit for the application. One example is that a user can write a python script like: player = Player() game.loadPlayer(player) player.moveTo(location)
4
3991
by: Jan | last post by:
Yesterday I got the idea to use remoting for the test interface between our Windows Xp Embedded - based device and our system test application. I started looking in the MSDN help and made the code below. My server class (RemotingServer) is located in one assembly (LibS.dll) used by the device software and the test system will use another assembly (LibC.dll) including my client class (TestConnectionRemoting). We have NO security issues -...
1
2563
by: Robert J. Bonn | last post by:
I'm trying to set up a contact list in MS Access 97. I've looked through a reference book and the program's help screens, but the light bulb isn't quite coming on for me. If one of you could take the time to answer two very elementary questions, I'd appreciate it. Suppose the contact list consists of (for example) musicians and teachers. For every person in the list, we keep name, address, etc. For musicians, we keep track of what...
0
9628
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9464
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9923
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7471
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6722
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4031
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2860
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.