interface design question

  • Thread starter Thread starter neverstill
  • Start date Start date
N

neverstill

Hi-

I designed a control and although I'm the only one that will be using it, I
thought it a good idea to create an interface for it.

First question is how do you add fields to an interface?
I tried:
private interface ITest
{
string Name;
}

and I get a compile error on that.

Are you not supposed to add Fields to an interface?

Thanks for any help,
Steve
 
Are you not supposed to add Fields to an interface?

No, because an interface defines a contract. You do not have an "instance of
an interface" like you have instances of classes. You *are* however able to
define properties which then must be implemented in a class:

interface ITest {
string Name { get; set; }
}

public class MyTest : ITest {
private string name;
public string Name {
get { return this.name; }
set { this.name = value; }
}
}
 
perfect! That is actually what I wanted, I wanted to enforce the
properties, not the members themselves.
Thanks for the sample, I'm good to go now!
 
Hi

Interface can only events, properties and methods. Fields or private member variables are not allowed in a interface
 
Back
Top