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
 

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