Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circular definition in a constant enum

Tags:

c#

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?

like image 342
MuriloKunze Avatar asked Oct 04 '12 14:10

MuriloKunze


1 Answers

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.

like image 56
MiMo Avatar answered Oct 04 '22 15:10

MiMo