views:

3621

answers:

7

Hi, i have problem with enum

I need make a enum in base class or interface (but empty one)

class Base 
{
   public enum Test;
   // ???
}

and after make diffrent enums in some parent classes

class Parent1
{
   public enum Test {A, B, C};
}

class Parent2
{
   public enum Test {J, H, K};
}

and now i have next class with method when i have to use enum

class Test<T>
{
   public void Foo(Test enum)
   {
      int value = (int) enum;
      // ...
   }
}

It's there any way to do something like that ?

If not i have to use static ints in every class ...

class Parent1
{
   public static int A = 0;
   public static int B = 5;
   public static int C = 7;
}

class Parent2
{
   public static int J = 1;
   public static int H = 3;
   public static int K = 6;
}

class Test<T>
{
   public void Foo(int enum)
   {
      int value = enum;
      // ...
   }
}

I't looks bad in code ... in some classes i have to use ~20+ variables

+1  A: 

No, there is no way to enforce anything static in an interface.

Perhaps you need to rethink your design.

Jon Grant
+3  A: 

There is no such thing as an abstract enum (that can have different implementations in subclasses) - but generics may be an option:

class Base<T> where T : struct {
    private T value;
    public void Foo(T value) {
        this.value = value;
    }
}
class Parent1 : Base<Parent1.Enum1> {
    public enum Enum1 {A, B, C};
}
class Parent2 : Base<Parent2.Enum2> {
    public enum Enum2 { J, H, K };
}

The only problem is that this doesn't enforce that only enums are usable - you can do this at runtime, though - for example in a type initializer:

static Base() {
    if (!typeof(T).IsEnum) throw new InvalidOperationException(
         typeof(T).Name + " is not an enum");
}
Marc Gravell
well its best possible solution, i can't use only one thing:abstract public class DB<T> where T : Base<T.Enum>, Interf, new()i understand why, i have to use static int's not enums then
kaem
A: 

Why cant you define the enum in base class:

class Base 
{
   public enum Test {A, B, C, J, H, K};
}

And use only the relevant members of enum in derived classes?

Hemant
A: 

No, it can't be done.

Note that many enums often indicate a problem with design (many switch constructs, for example). Check this link to see an example of how to refactor this: Replace conditional with polymorphism.

Groo
+1  A: 

You should be able to declare an enum in a base class and then change the values per derived class i.e.

class MyClass
{
    public enum TestEnum { }

    public MyClass()
    {
    }
}

class MyDerivedClass
{
    public enum TestEnum { value1, value2, value3 }

    public MyDerivedClass()
    {
    }
}

MyDervied class would have access to TestEnum.value1, TestEnum.value2, TestEnum.value3, where as MyClass would only have access to the type.

However, personally I don't see the advantage of doing this I would declare ALL the values of the enum in the base class and only use the ones I need per class.

James.

James
Problem is - not me create child classes, but some users, and i don't want them to modify main class adding values to this enum
kaem
hmmm and i have a warrning if i use this solution, but its possible to do that:new public enum TestEnum { value1, value2, value3 }hmmmm
kaem
Yeah the more I think about what it is your trying to do its not possible. As by declaring new public enum TestEnum will create a separate type of enum to the base class. I think the correct solution would be to have an enum in the base class only.
James
seem's like i can't do something like that:class Test<T> where T : MyClass{ public Foo(T.TestEnum te) ...}becose it's should be in the interface, and i can't declare types in interface
kaem
but you can just store the enum value as an integer and declare that in the interface instead?
James
A: 

And if someone thinking why i trying use something like that: I have for example Class

class PersonalData 
{
   int id;
   string name;
   int group;
}

and a base class ...

abstract public class DB<T> where T : Base, Interface, new()
{
   // ...
   public List<T> Select(int column, object value)
   {
      T tmp = new T();
      FieldInfo field;
      string query;
      try
      {
         field = tmp.GetFields()[column];
      }
      catch
      {
         // TODO - bad column
         return list;
      }
      if (value.GetType() != field.GetValue(tmp).GetType())
      {
         // TODO - add error
         return list;
      }
      // generating a query
      return list;
   }
}

Then i'm making something like that:

class PersonalDataDB : DB<PersonalData>
{
   // ...
}

And i need enum to do something like that:

PersonalDataDB pdb = new PersonalDataDB();
pdb.Select(PersonalData.Group, 3);
pdb.Bind(dataGridView1);

well, dont ask my why i have to do like that, it wasn't my idea

kaem
A: 

It's amazing how often I find people arguing about why something is required, rather than answering the question asked or keeping schtum - either of which would be more helpful than wasting time questioning why a given enquiry has been made in preference to a different enquiry the respondent actually knows the answer to. Answering questions that have not been asked is in no way helpful, OK guys?!

Getting back to the topic at hand, I've hit exactly the above scenario this morning, and can understand why it would be useful to be able to define an Enum in an interface or base class, then re-define that same-named Enum in a class that derives from either the base or interface. One use for such a design is in object-relational mapping and control binding. You might have a set of Enums that describe which Properties of your derived classes are bindable to which types of Control, such as:

    public enum WebControlTextBoxProperties { }

    public enum WebControlLabelProperties { }

...and so on.

Since you don't know exactly which Properties will exist for a given derived class until the pertinent inheritance takes effect, but since you may also wish to have consistent Methods that consume the above Enums in your base or interface, it's a perfectly-valid design to expect to be able to define that the Enum will exist in the base/interface, but define exactly what members it has in a specific context in any derived classes.

I really wish this were possible in C# as it is in VB, because it'd be a really useful feature.

Rachel