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

Purpose of using interfaces in .net.

Hi All,

Can anyone please tell me how interface is going to help me in real time
senarios.
As interface contains only skeleton of methos and it has to be inherited to
a class.

Is there anything which we can achive only through interface, Please help me
in this as i am a bit confused.

Hope my question is clear.
Thanks in advance

Regards
Mani.
Nov 28 '06 #1
4 1716
If you are working with an object that implements a specific
interface, you can cast to that interface and use the methods
already developed for it.

You can implement the IComparable interface and then
write the code CompareTo function for it, enabling
you to sort, say, a list of objects.

You can implement the ICloneable interface and write
the function Clone to clone your business object. This
is implemented by ArrayList, Font, Icon, Queue, and Stack.
When others are using your class, this enables them to
clone the business object without having to know all
the internals.
The following example came from Brian Noyes' Data Binding book.
There's a lot of interesting stuff like this. Like you can cast a
collection to ITypedList and then get the item properties for the
items in the list.

Anyway, here's a specific example, complete with code:

Say you have a dataset and you want to raise an event
that something in the dataset changed.

For this example, I have a DataSet called NorthwindDataset
defined for the NorthWind database with the Customers table in it:

class Program
{
static void Main(string[] args)
{
//get some data to work with
NorthwindDataSet nwData = new NorthwindDataSet();
CustomersTableDataAdapter adapter = new CustomersTableAdapter();
adapter.Fill(nwData.Customers);

//Get an iBindingList interface reference
IBindingList list = nwData.Customers.DefaultView;
//subscribe to change events
list.ListChanged += new ListChangedEventHandler(OnListChanged);

//Delete a row
list.RemoveAt(1);

//Add a column
nwData.Customers.Columns.Add("New Column", typeof(string));

//Change an item in the collection
nwData.Customers[0].CompanyName = "myCompany";
}

//subscribe to the event
static void OnListChanged(object sender, ListChangedEventArgs e)
{
Console.WriteLine("ListChangedType Value: {0}",
e.ListChangedType);
Console.WriteLine("NewIndex value: {0}", e.NewIndex);
Console.WriteLine("OldIndex value: {0}, e.OldIndex);
If (e.PropertyDescriptor != null)
{
Console.WriteLine("PropertyDescriptor Name: {0}",
e.PropertyDescriptor.Name);
Console.WriteLine("PropertyDescriptor Type: {0}",
e.PropertyDescriptor.PropertyType);
}
Console.WriteLine;
}
}

I'm not a C# programmer, so if I got any of the curly braces
wrong or missed a semi-colon, please don't castigate me.
If you want this translated to VB, I can do that.

Here's the output:

ListChangedType Value: ItemDeleted
NewIndex value: 1
OldIndex value: -1

ListChangedType Value: PropertyDescriptorAdded
NewIndex value: 0
OldIndex value: 0
PropertyDescriptor Name: New Column
PropertyDescriptor Type: System.String

ListChangedType Value: ItemChanged
NewIndex value: 0
OldIndex value: 0
PropertyDescriptor Name: CompanyName
PropertyDescriptor Type: System.String

Hope that helps.
Robin S.
"Mani" <Ma**@discussions.microsoft.comwrote in message
news:82**********************************@microsof t.com...
Hi All,

Can anyone please tell me how interface is going to help me in real time
senarios.
As interface contains only skeleton of methos and it has to be inherited
to
a class.

Is there anything which we can achive only through interface, Please help
me
in this as i am a bit confused.

Hope my question is clear.
Thanks in advance

Regards
Mani.

Nov 28 '06 #2
Robin: Rather than illustrate by example may I offer the explanation that an
interface is a "contract". A class which chooses to implement an interface
agrees to supply a defined set of functionality and consumers of objects
based upon that class know they can interact with an object via the
interface. It is different from subclassing as C# and VB.Net only support
single inheritance (classes based upon a single base class) but both
languages support multiple interfaces.

It is such an important a concept that "programming to the interface" has
become a basic tenent of OOP development.

Tom

"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:Z-******************************@comcast.com...
If you are working with an object that implements a specific
interface, you can cast to that interface and use the methods
already developed for it.

You can implement the IComparable interface and then
write the code CompareTo function for it, enabling
you to sort, say, a list of objects.

You can implement the ICloneable interface and write
the function Clone to clone your business object. This
is implemented by ArrayList, Font, Icon, Queue, and Stack.
When others are using your class, this enables them to
clone the business object without having to know all
the internals.
The following example came from Brian Noyes' Data Binding book.
There's a lot of interesting stuff like this. Like you can cast a
collection to ITypedList and then get the item properties for the
items in the list.

Anyway, here's a specific example, complete with code:

Say you have a dataset and you want to raise an event
that something in the dataset changed.

For this example, I have a DataSet called NorthwindDataset
defined for the NorthWind database with the Customers table in it:

class Program
{
static void Main(string[] args)
{
//get some data to work with
NorthwindDataSet nwData = new NorthwindDataSet();
CustomersTableDataAdapter adapter = new CustomersTableAdapter();
adapter.Fill(nwData.Customers);

//Get an iBindingList interface reference
IBindingList list = nwData.Customers.DefaultView;
//subscribe to change events
list.ListChanged += new ListChangedEventHandler(OnListChanged);

//Delete a row
list.RemoveAt(1);

//Add a column
nwData.Customers.Columns.Add("New Column", typeof(string));

//Change an item in the collection
nwData.Customers[0].CompanyName = "myCompany";
}

//subscribe to the event
static void OnListChanged(object sender, ListChangedEventArgs e)
{
Console.WriteLine("ListChangedType Value: {0}",
e.ListChangedType);
Console.WriteLine("NewIndex value: {0}", e.NewIndex);
Console.WriteLine("OldIndex value: {0}, e.OldIndex);
If (e.PropertyDescriptor != null)
{
Console.WriteLine("PropertyDescriptor Name: {0}",
e.PropertyDescriptor.Name);
Console.WriteLine("PropertyDescriptor Type: {0}",
e.PropertyDescriptor.PropertyType);
}
Console.WriteLine;
}
}

I'm not a C# programmer, so if I got any of the curly braces
wrong or missed a semi-colon, please don't castigate me.
If you want this translated to VB, I can do that.

Here's the output:

ListChangedType Value: ItemDeleted
NewIndex value: 1
OldIndex value: -1

ListChangedType Value: PropertyDescriptorAdded
NewIndex value: 0
OldIndex value: 0
PropertyDescriptor Name: New Column
PropertyDescriptor Type: System.String

ListChangedType Value: ItemChanged
NewIndex value: 0
OldIndex value: 0
PropertyDescriptor Name: CompanyName
PropertyDescriptor Type: System.String

Hope that helps.
Robin S.
"Mani" <Ma**@discussions.microsoft.comwrote in message
news:82**********************************@microsof t.com...
>Hi All,

Can anyone please tell me how interface is going to help me in real time
senarios.
As interface contains only skeleton of methos and it has to be inherited
to
a class.

Is there anything which we can achive only through interface, Please help
me
in this as i am a bit confused.

Hope my question is clear.
Thanks in advance

Regards
Mani.


Nov 28 '06 #3
"Mani" <Ma**@discussions.microsoft.coma écrit dans le message de news:
82**********************************@microsoft.com...

| Can anyone please tell me how interface is going to help me in real time
| senarios.
| As interface contains only skeleton of methos and it has to be inherited
to
| a class.

What you say here is a common misconception. Classes do not inherit
interfaces, they implement them; there is a difference.

An interface is simply a contract, or definition of expected behaviour.

e.g.

IComparable - the implementing class must provide a means of comparing
instances of that type.

IEnumerable - the implementing class must provide a means of accessing an
enumerator or iterator.

Unlike inheriting a class, which has to be done within a hierarchy where
everything derives from a base class, implementing an interface can be done
across hierarchies, withouot having to derive everything from the same base
class.

e.g.

class Animal : IComparable {...}

class Vehicle : IComparable {...}

These two classes have nothing in common but we want to be able to treat
them identically for purposes of comparison, so we get them both to
implement the same interface, thus allowing us to assign an instance of
either Animal or Vehicle to an IComparable interface reference and to call
the CompareTo method, regardless of what the real type of the instance is.

| Is there anything which we can achive only through interface, Please help
me
| in this as i am a bit confused.

You can implement more than one interface in a class, but you could only
inherit from one other class.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Nov 28 '06 #4
I was thinking of consuming interfaces, rather than the
other side, of using them when developing your own classes,
to ensure that the consumer of those classes implemented
the appropriate methods and/or properties. You're right,
of course.

Robin S.
---------------------
"Tom Leylan" <ge*@iamtiredofspam.comwrote in message
news:et**************@TK2MSFTNGP02.phx.gbl...
Robin: Rather than illustrate by example may I offer the explanation that
an interface is a "contract". A class which chooses to implement an
interface agrees to supply a defined set of functionality and consumers of
objects based upon that class know they can interact with an object via
the interface. It is different from subclassing as C# and VB.Net only
support single inheritance (classes based upon a single base class) but
both languages support multiple interfaces.

It is such an important a concept that "programming to the interface" has
become a basic tenent of OOP development.

Tom

"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:Z-******************************@comcast.com...
>If you are working with an object that implements a specific
interface, you can cast to that interface and use the methods
already developed for it.

You can implement the IComparable interface and then
write the code CompareTo function for it, enabling
you to sort, say, a list of objects.

You can implement the ICloneable interface and write
the function Clone to clone your business object. This
is implemented by ArrayList, Font, Icon, Queue, and Stack.
When others are using your class, this enables them to
clone the business object without having to know all
the internals.
The following example came from Brian Noyes' Data Binding book.
There's a lot of interesting stuff like this. Like you can cast a
collection to ITypedList and then get the item properties for the
items in the list.

Anyway, here's a specific example, complete with code:

Say you have a dataset and you want to raise an event
that something in the dataset changed.

For this example, I have a DataSet called NorthwindDataset
defined for the NorthWind database with the Customers table in it:

class Program
{
static void Main(string[] args)
{
//get some data to work with
NorthwindDataSet nwData = new NorthwindDataSet();
CustomersTableDataAdapter adapter = new CustomersTableAdapter();
adapter.Fill(nwData.Customers);

//Get an iBindingList interface reference
IBindingList list = nwData.Customers.DefaultView;
//subscribe to change events
list.ListChanged += new ListChangedEventHandler(OnListChanged);

//Delete a row
list.RemoveAt(1);

//Add a column
nwData.Customers.Columns.Add("New Column", typeof(string));

//Change an item in the collection
nwData.Customers[0].CompanyName = "myCompany";
}

//subscribe to the event
static void OnListChanged(object sender, ListChangedEventArgs e)
{
Console.WriteLine("ListChangedType Value: {0}",
e.ListChangedType);
Console.WriteLine("NewIndex value: {0}", e.NewIndex);
Console.WriteLine("OldIndex value: {0}, e.OldIndex);
If (e.PropertyDescriptor != null)
{
Console.WriteLine("PropertyDescriptor Name: {0}",
e.PropertyDescriptor.Name);
Console.WriteLine("PropertyDescriptor Type: {0}",
e.PropertyDescriptor.PropertyType);
}
Console.WriteLine;
}
}

I'm not a C# programmer, so if I got any of the curly braces
wrong or missed a semi-colon, please don't castigate me.
If you want this translated to VB, I can do that.

Here's the output:

ListChangedType Value: ItemDeleted
NewIndex value: 1
OldIndex value: -1

ListChangedType Value: PropertyDescriptorAdded
NewIndex value: 0
OldIndex value: 0
PropertyDescriptor Name: New Column
PropertyDescriptor Type: System.String

ListChangedType Value: ItemChanged
NewIndex value: 0
OldIndex value: 0
PropertyDescriptor Name: CompanyName
PropertyDescriptor Type: System.String

Hope that helps.
Robin S.
"Mani" <Ma**@discussions.microsoft.comwrote in message
news:82**********************************@microso ft.com...
>>Hi All,

Can anyone please tell me how interface is going to help me in real time
senarios.
As interface contains only skeleton of methos and it has to be inherited
to
a class.

Is there anything which we can achive only through interface, Please
help me
in this as i am a bit confused.

Hope my question is clear.
Thanks in advance

Regards
Mani.



Nov 28 '06 #5

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

Similar topics

21
by: Hattuari | last post by:
I'm learning C++ after having spent several years in the computer industry doing both system administration and engineering. I've written code in Perl, Bash, Pascal, Ada, C, Mathematica (hundreds...
3
by: Amit chaturvedi | last post by:
Dear Friends Please tell me following thing Que -: what is the main use of interface in .net Que -: What is difference between abstract class and interface Que -: How to make class in Object...
27
by: jm | last post by:
I am having trouble understanding the purposes of an interface, even though the concept of interfaces is around me all the time (user interface, for example). I'm just not understanding software...
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: 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:
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?
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...
0
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...

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.