Thanks so much.
Steve
They are the beginning of a long journey on the road of OO (Object Oriented)
Programming.
..
"S_K" <steve_...@yahoo.com> wrote in message
news:757d2999-9101-4f03...@z16g2000prn.googlegroups.com...
The easiest way to understand interfaces is to think of them as a contract.
When an object implements an interface it "contracts" to implement the
methods etc of the interface.
This means that if you have several object which all implement a certain
interface you can use the interface as the variable type.
Following is VB but same in C#.
Lets say I have an object class animal. I do not want to have talking
animals (make a sound) and non talking animals but I will implement the
IMakeASound interface for animals that make a sound. This is important
since C# and VB.Net do not allow for multiple inheritance. Interfaces allow
for an object to overcome this.
I would then be able to have:
interface IMakeASound
sub Talk()
end interface
dim myTalkingAnimals as new List(of IMakeASound)
I could then add talking animals to this list and do the following:
for each animal in myTalkingAnimals
animal.Talk()
next
This is just a start.
LS
So each class implements the same interface eg:
public class Dog: IMakeASound
{
Talk()
{ return "bow wow"}
}
public class Cat: IMakeASound
{
Talk()
{ return "meow"}
}
Then you can use this interface in a seperate class:
List<IMakeASound> animal = new List<IMakeASound>();
animal.Add(new new Cat());
animal.Add(new Dog());
string talk1 = animal[0].Talk();
string talk2 = animal[1].Talk();
talk1 is "meow"
talk2 is "bow wow"
THAT IS SOOOO COOL!
Thanks so much!!!
THAT IS SOOOO COOL!
Thanks so much!!!
Where it gets really cool is in thing like the data classes. There are a
bunch of interfaces that each data class implement. This means that you can
code a system lets say using Access and very quickly (if you only use the
interfaces) code the same project with SQL Server. Check out things like
IDDbCommand, IDataAdapter etc.
LS
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!126.entry
it'll show you (one) way to use Interfaces.
..
"sloan" <sl...@ipass.net> wrote in message
news:e4vH1jes...@TK2MSFTNGP06.phx.gbl...
say you wanted to create a new business object collection class that
inherited from ArrayList (collection) and your base business object methods.
in python, you just inherit from both classes and your done.
in ruby (which does not support multiple inheritance) you just create
business methods with the same name and parameters. in either case, you could
pass the collection to code that knew how to call the business object and it
would work. not in c#, it would throw a type error.
in c# your business object defines an interface, and another class
implements it. then that object can be cast to the interface and the methods
called.
-- bruce (sqlwork.com)
A book that deals directly with your question, is Programming .NET
Components by Juval Lowy. Just chapters 1 and 3 are all you really need in
order to gain a clear understanding and appreciation of Interfaces and
their pervasive role in modern object-oriented programming.
One of the best books, by far, that can help you really "get" some of the
many powerful uses of interfaces Head First Design Patterns by Eric Freeman
and Elizabeth Freeman.
-HTH
"S_K" <steve_...@yahoo.com> wrote in message
news:757d2999-9101-4f03...@z16g2000prn.googlegroups.com...