Q: Yes its another interface question for a dummy like me.

  • Thread starter Thread starter Martin Arvidsson \(Visual Systems AB\)
  • Start date Start date
M

Martin Arvidsson \(Visual Systems AB\)

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
 
Martin Arvidsson (Visual Systems AB) wrote:

why on earth would i need or should i use an interface like this
interface MyInterface
{
stinrg Text
{
get;
set;
}
}

and declare myclass : myinterface?

Because then in various cases you could write the calling code to only
know that it needed an implementation of MyInterface, rather than your
concrete class.

Look at IDisposable - all kinds of classes need to implement it, and
using an interface means they don't all need to derive from the same
concrete class.

Jon
 
Hi,

In this specific situation it wouldn't be interesting at all.

It would become interesting as soon as you have more controls that all
have a 'Text' property, and you'd have to write a method that works on
objects with a Text property.

Consider the following, this will only work if both classes implement
IMyInterface:

public class MyClass3{
public method Test(){
WriteText(new MyClass2());
WriteText(new MyClass3());
}
public method WriteText(IMyInterface interface){
Console.WriteLine(interface.Text)
}
}

public class MyClass2 {
string _test;
public string Text{get{return _text}};
}

public class MyClass3 {
string _test;
public string Text{get{return _text}};
}

interface IMyInterface
{
string Text
{
get;
}
}

but there are so many design considerations to use interfaces, I'd start
looking at how some design patterns and best practices work:
http://codebetter.com/blogs/david.h..._-Castle_2700_s-Windsor_2F00_MicroKernel.aspx
http://en.wikipedia.org/wiki/Design_Patterns
http://en.wikipedia.org/wiki/Law_of_Demeter

Regards,

Wiebe Tijsma
http://www.e-office.com
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top