0

The following doesn't work because w and h need to be const's. I'm assuming Enums have to be set at compile time and can't be changed at run time. Is this a possibility, or is it better to just make foo a class?

class Bar 
{
    int w, h;
    enum foo : int 
    { 
        a = w * h 
    }

    public Bar(int w, int h) 
    {
         this.w = w;
         this.h = h;
    }
}
3
  • 1
    Yes, enum member definitions must be compile time constants. Whether or not foo is better suited to be a class depends entirely upon how you're going to use it, really. Commented Nov 30, 2015 at 17:24
  • 2
    Enumerations shouldn't derive from int explicitly: they already do it implicitly... Commented Nov 30, 2015 at 17:24
  • Your ideas regarding how to use enums seem quite unclear. Here you have an example which might be helpful: enum myFirstInts { one, two, three } which you can use like myFirstInts myFirstEnumVar = myFirstInts.one; or even myFirstInts myFirstEnumVar = 0; (-> it refers to one). Commented Nov 30, 2015 at 17:28

2 Answers 2

1

It seems that you are not completely getting the idea of enums in c#. Take a look at the C# Reference (here)

The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list.

You cannot assign variables to an enum. You should be looking at another structure.

Sign up to request clarification or add additional context in comments.

Comments

0

as you say, you can't: an enum must be defined at compile time, not at runtime. Don't go with a different class, just make foo a read only property:

public int foo
{
    get
    {
         return w*a;
    }
}

1 Comment

foo will actually hold more than one value. That's why its an enum in the first place. ignore the fact the pseudo code only holds one value. So although the answer is technically correct, the example doesn't solve my problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.