Hi!
>
I realy dont get the point of interfaces
If i have this code
Public class MyClass
{
Public MyClass()
{
}
Public string Text
{
Get { return xxx;}
Set { xxx = value;}
}
}
why on earth would i need or should i use an interface like this
interface MyInterface
{
stinrg Text
{
get;
set;
}
}
and declare myclass : myinterface?
I just don't see the point of doing this. I do the code once more anyhow in
the class...
Yes i am stupid, thats why i am asking ;)
Regards
Martin
When you have just that one class, probably you don't need that
interface.
Interfaces are for situations like this:
* you have two (or more) classes, say MyClass and MyOtherClass. Both
contain a Text property.
* for some reason they can not be derived from the same baseclass
* you have some method that takes some object and does something to
it's Text property
then you could declare an interface:
interface IHasTextProperty
{
string Text
{
get;
set;
}
}
declare your classes MyClass and MyOtherClass to implement that
interface:
class MyClass: IHasTextProperty
{}
then you can create that method as
public void AddText(IHasTextProperty TheClass, string NewText)
{
TheClass.Text = NewText;
}
and use it like
MyClass x = new MyClass();
AddText(x, "foo");
MyOtherClass y = new MyOtherClass();
AddText(y, "bar");
an interface is often referred to as a "contract": the compiler now
knows that a class that implements that interface does have these
methods and/or properties (and doesn't care about any other
properties).
A real life example would be IDisposable: a class that implements this
can (and should) be Dispose()d.
Hans Kesting