Purpose of using interfaces in .net.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
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.
 
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 said:
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 said:
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.
 
"Mani" <[email protected]> a écrit dans le message de (e-mail address removed)...

| 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
 
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 said:
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 said:
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 said:
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.
 
Back
Top