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.