473,725 Members | 2,276 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Whats an interface?

Hi,

Im new in this of the .net, and more or less i get the concepts of classes
events, delegates, but still i dont understand in wich kind of programs the
interfaces are very useful...

I would like to have a clear vision about the use of this and in wich cases
are better the use.

Could you put some examples (no neccesary code, only examples as ideas)
about when the use of interface its the best way to follow?

Any help would be grateful...
Thanks.
Nov 16 '05 #1
4 1442
If you want to create a UserControl that show an array of object you don't
know wath property of the object you have to show... for example...

You create the IMyUserControl in wich you create the ShowObj() method.

Every class you want could be showed in your UC must implements the
IMyUserContol interface.

In the UserControl, when you have to show the object, you use the interface:

((IMyUserContro l)theObjectToSh ow).ShowObj()

ta... da...

Sorry for my english...
Nov 16 '05 #2
For example, you could write a sorting algorithm that uses interfaces (e.g.
IList and IComparable) to access the data to be sorted: That way the same
code could be used to sort an array, an ArrayList or some linked list class,
as long as each of them implement the same interface.

You can do something similar with abstract base classes; The advantage of
interfaces is that a class can have only one direct base class, but can
implement any number of interfaces.

Interfaces are also quite commong for "loose-coupling" parts of big
applications: Each application part only exposes a set of interfaces, the
internals (like what each class implements, what it's derived from) stay
hidden;

Niki

"Josema" <Jo****@discuss ions.microsoft. com> wrote in
news:94******** *************** ***********@mic rosoft.com...
Hi,

Im new in this of the .net, and more or less i get the concepts of classes
events, delegates, but still i dont understand in wich kind of programs
the
interfaces are very useful...

I would like to have a clear vision about the use of this and in wich
cases
are better the use.

Could you put some examples (no neccesary code, only examples as ideas)
about when the use of interface its the best way to follow?

Any help would be grateful...
Thanks.

Nov 16 '05 #3
Hi Josema,

Let's understand the concept behind interfaces with an example. :)

We know of several birds that can fly...like sparrows, doves, crows etc. We
also know of several other entities that can fly...like kites, planes,
shuttles, rockets etc. But the way a sparrow flies is completely different
from the way a plane does. The way a plane flies is also completely different
from the way a shuttle does...We are sure of one thing but...all these
entities can FLY.
In terms of programming, we would define an interface IFlyingObjects with
the method Fly. And then would create seperate classes for each of the
examples above, each of which would implement the interface IFlyingObjects.
This would ensure that each of the object would have defined a method Fly
with their own specific implementation.

Interfaces are something the qualifies an entity. For example, calling a
person an actor would automatically mean that he/she is capable of *Acting*.
Calling another person a painter would mean he/she is capable of *Painting*.
But these people are all *People* as well - only the different qualities vary.

As per the programmatic definition, Interfaces define a contract - which
means that if a class implements an interface, we can be sure that it defines
all the members declared in the interface.

HTH,
- Rakesh

"Josema" wrote:
Hi,

Im new in this of the .net, and more or less i get the concepts of classes
events, delegates, but still i dont understand in wich kind of programs the
interfaces are very useful...

I would like to have a clear vision about the use of this and in wich cases
are better the use.

Could you put some examples (no neccesary code, only examples as ideas)
about when the use of interface its the best way to follow?

Any help would be grateful...
Thanks.

Nov 16 '05 #4
Josema,

I usually think of interfaces as something to consider when I have a
particular signature of methods and/or properties that need to be consistent
across two or more classes that are not in the same inheritance tree; or
where it represents a bit of functionality that you want to be optional for
specific concrete classes.

A good example of the first situation is IDisposable. Any random class can
implement IDisposable if it needs to be responsible for unmanaged resources.
This is functionality that has wide applicability and describes behaviors
that are fairly generic.

One of the handy uses of interfaces is that you can cast to them like you
can a "real" type. So this enables code such as the following, which might
be used when iterating through a collection of objects, some of which are
disposable:

if (someObject is IDisposable) {
((IDisposable)s omeObject).Disp ose();
}

Without the interface cast you would have some huge switch statement that
would have to cover every possible disposable type that might be
encountered, which would be error-prone, tedious and fragile.

--Bob

"Josema" <Jo****@discuss ions.microsoft. com> wrote in message
news:94******** *************** ***********@mic rosoft.com...

Im new in this of the .net, and more or less i get the concepts of classes
events, delegates, but still i dont understand in wich kind of programs the interfaces are very useful...

Nov 16 '05 #5

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

Similar topics

3
5361
by: Kevin Steffer | last post by:
Hi group I have a webform which I want to make an ftp connection for a filetransfer from. The thing is when I use the WebRequest class it says "The URI prefix is not recognized" and my URI is ftp://localhost/ Then I discovered that the WebRequest class has a RegisterPrefix method which takes a string as prefix and it needs a System.Net.IWebRequestCreate but how is such one made ?
18
3855
by: smileplzz | last post by:
r there any files which we can download from this group. can we only ask questions in this group? no files stored like it is there in yahoo groups. i think it should be there in group services. ur new group member Ram 20/m/India
3
2646
by: Peter Q | last post by:
As the title suggests, what are the differences between a web app and a web service? What exactly IS a web service?? thanks Peter
21
2033
by: Paul Tremblay | last post by:
Hi All, I am a veteran C/C++ programmer (Unix background) and I want to get to speed with Visual Studio .Net I have legacy C/C++ code that I want to use in my application. However, I'm not sure how to call my native C++ functions from my C++ DLLs. The confusion is this: I currently have Visual Studio 2003 it is not clear whether I should use managed extensions or C++/Interop?
2
2571
by: Showjumper | last post by:
Whats the diff between the 2 and when do you use one and not the other?
3
1714
by: Lucas Tam | last post by:
Does anyone have a good articles that describes the pros and cons of Web Services vs. Remoting Hosted in IIS? Is there a reason to use either or? With Remoting Hosting in IIS, is it possible to maintain a constant thread (i.e. thread that polls the database and sends messages back to client when a certain record is found?). Thanks.
20
2460
by: Snis Pilbor | last post by:
Whats the point of making functions which take arguments of a form like "const char *x"? It appears that this has no effect on the function actually working and doing its job, ie, if the function doesn't write to x, then it doesnt seem like the compiler could care less whether I specify the const part. Quite the opposite, if one uses const liberally and then later goes back and changes the functions, headaches will inevitably occur as...
4
3989
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 -...
7
2274
by: Paulo | last post by:
Hi, what is diference between: File -New Web Site and File -New Project -VB/C# -Web Application ?????? VS 2005
0
8752
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
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9179
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6702
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
6011
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();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3228
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
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.