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?

3
  • relevant? stackoverflow.com/questions/211567/… Commented Oct 4, 2012 at 14:14
  • 4
    If it hurts when you do that, stop doing that. Commented Oct 4, 2012 at 14:30
  • 2
    Very interesting. Note that if you remove const (possibly specifying static and/or readonly instead), it works! C# Language Specification section 7.6.4.1 applies in most cases, but why not in yours? Commented Feb 27, 2015 at 13:23

4 Answers 4

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.

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

7 Comments

And the solution is to change ActivityStatus.Open to Namespace.ActivityStatus.Open
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...)
@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, I'm not sure all agree stackoverflow.com/questions/211567/…
@Servy: I don't agree. There more than enough samples of this. No sign of trouble.
|
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; } 

5 Comments

you can. this problem is specific to const
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.
Well, you can, but you never should, even if there is some way to disambiguate the variable from the type when using them.
@Servy, I'm not sure all agree stackoverflow.com/questions/211567/…
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.
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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.