17

I'm trying to create a constant of type Enum but I get a error.. My enum is:

public enum ActivityStatus
{
    Open = 1,
    Close = 2
}

and I have a model that uses it:

public class CreateActivity
{
    public int Id;
    public const ActivityStatus ActivityStatus = ActivityStatus.Open;
}

the following error occurs:

Error 1 The evaluation of the constant value for 'Help_Desk.Models.CreateActivity.ActivityStatus' involves a circular definition...

But if I change the name of ActivityStatus property it works!

public class CreateActivity
{
    public int Id;
    public const ActivityStatus AnyOtherName = ActivityStatus.Open;
}

Why it happens?

Liam
  • 27,717
  • 28
  • 128
  • 190
MuriloKunze
  • 15,195
  • 17
  • 54
  • 82

4 Answers4

17

Because the c# compiler intepretes the third ActivityStatus in:

public const ActivityStatus ActivityStatus = ActivityStatus.Open; 

as the name of the constant being defined instead than the name of the enumeration - hence the circular reference: you are definining a constant in terms of the constant itself.

In C# you can use the same name for members and types, and usually resolve ambiguities specifying the fully qualified names (i.e. adding the namespace), but in my experience it is not a good idea, it makes code confusing: the compiler can figure out which is which, but the poor human reading the code has an hard time figuring out if a certain name refers to a class or type or member.

MiMo
  • 11,793
  • 1
  • 33
  • 48
  • 2
    And the solution is to change `ActivityStatus.Open` to `Namespace.ActivityStatus.Open` – Daniel Hilgarth Oct 04 '12 at 14:16
  • 1
    No, it is not a compiler bug - the problem is that third `ActivityStatus` can mean two things: the name of the enumeration or the name of the constant, the compiler has to pick one - and it picks the name of the constant (if @EricLippert shows up he can explain us why the compiler makes this choice...) – MiMo Oct 04 '12 at 14:20
  • 1
    @DanielHilgarth No, that's not the solution. Having a variable of the same name as it's type is just asking for trouble. The solution is to change the name of the variable. – Servy Oct 04 '12 at 14:28
  • 2
    @Servy, I'm not sure all agree http://stackoverflow.com/questions/211567/enum-and-property-naming-conflicts – Jodrell Oct 04 '12 at 14:32
  • 2
    @Servy: I don't agree. There more than enough samples of this. No sign of trouble. – Daniel Hilgarth Oct 04 '12 at 14:33
  • @murilokunze: I try to avoid it, but that's just my personal preference. It is fine to do it if you prefer - and as others pointed out above there are even example of this in the system libraries (the `System.Data.CommandType` enumeration and the `System.Data.DbCommand.CommandType` property) – MiMo Oct 04 '12 at 16:35
  • 1
    It a little curious to me that the compiler (in the parser?) decides that the third `ActivityStatus` is the constant being defined. If it were less greedy and parsed the whole statement, surely it would be valid? On the face of it, the rules are simple (probably @EricLippert would show they're not) to figure out a fallback strategy on the the assignment. Better read the spec and see what it says. In other places, the compiler can infer the expected behaviour, why not here? – nicodemus13 Nov 09 '12 at 11:25
1

You should not create a variable with the same name of a class or enum.

Maybe it will work if you specify the namespace, like :

public class CreateActivity
{
    public int Id;
    public const TheNamespace.ActivityStatus ActivityStatus =
        TheNamespace.ActivityStatus.ActivityStatus.Open;
}
Servy
  • 202,030
  • 26
  • 332
  • 449
Anthony Simmon
  • 1,579
  • 12
  • 26
  • 1
    you can. this problem is specific to const – Peter Porfy Oct 04 '12 at 14:15
  • That's not correct. You can name your variables like classes, no problem. No downvote because using the namespace on `ActivityStatus.Open` solves the compiler error. – Daniel Hilgarth Oct 04 '12 at 14:16
  • Well, you *can*, but you never *should*, even if there is some way to disambiguate the variable from the type when using them. – Servy Oct 04 '12 at 14:29
  • 2
    @Servy, I'm not sure all agree http://stackoverflow.com/questions/211567/enum-and-property-naming-conflicts – Jodrell Oct 04 '12 at 14:31
  • I disagree very strongly with this answer. Naming a variable the same as it's enum type is _extremely_ common and not a bad practice at all IMHO. – julealgon Oct 23 '18 at 21:29
0

If private usage only: You can change ActivityStatus to readonly field and move setting of default value to constructor.

If public usage: You can use property with getter only.

In most of the code analyzers additional namespace will be treated as redundance in your code.

0

To avoid from the Circular Definition Problem in C#, You can reference/call the variable/type/member etc with full qualified name like

Namespace..Member/Type/ etc

Hope you will fix the error by now.

Tahir Alvi
  • 896
  • 2
  • 14
  • 44