bwoogie
bwoogie

Reputation: 4427

Setting Enum values with variables

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;
    }
}

Upvotes: 0

Views: 87

Answers (2)

Camilo Terevinto
Camilo Terevinto

Reputation: 32068

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.

Upvotes: 1

Gian Paolo
Gian Paolo

Reputation: 4249

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;
    }
}

Upvotes: 0

Related Questions