How to do an enumeration in an interface?

  • Thread starter Richard Lewis Haggard
  • Start date
R

Richard Lewis Haggard

I have a situation where I have to combine an enumerated value with an
interface. How can this be done?

namespace Stuff
{
public interface IThing
{
// This doesn't work, of course...
public enum EnumeratedStuff
{
None, First, Second
}

bool SomeMethod();
}
}

namespace Stuff
{
public class Thing : IThing
{
// Implement the enumerated EnumeratedStuff.

bool SomeMethod()
{
return true;
}
}
}
 
T

Tom Porterfield

Richard said:
I have a situation where I have to combine an enumerated value with an
interface. How can this be done?

namespace Stuff
{
public interface IThing
{
// This doesn't work, of course...
public enum EnumeratedStuff
{
None, First, Second
}

bool SomeMethod();
}
}

namespace Stuff
{
public class Thing : IThing
{
// Implement the enumerated EnumeratedStuff.

bool SomeMethod()
{
return true;
}
}
}

Do you mean something like this:

namespace Stuff
{
public interface IThing
{
bool SomeMethod();
EnumeratedStuff EnumStuff{ get;set; }
}

public enum EnumeratedStuff
{
None, First, Second
}
}

namespace Stuff
{
public class Thing : IThing
{
private EnumeratedStuff es = EnumeratedStuff.None;

public bool SomeMethod()
{
return true;
}

public EnumeratedStuff EnumStuff
{
get { return this.es; }
set { this.es = value; }
}
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

Richard,

You can't do that. The enumeration has to be defined outside of the
interface, and you can't declare that the interface has to declare an
enumeration type with that name.
 
R

Richard Lewis Haggard

Greatly appreciated. Your response told me exactly what I needed to know.
 

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

Top