Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Declare a static variable using an enumerator C# (.NET 6)

Just a simple question, but I cannot find it.

I have this code in 1 file:

namespace Models
{
    public enum MyEnvironment
    {
        Development,
        Testing,
        Acceptance,
        Production
    }
}

And in my program.cs file:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

global using Models
static MyEnvironment CurrentEnvironment = MyEnvironment.Development;

With this code I want to set a Environment that is global accessible through my whole code. But I get the error:

the modifier ‘static’ is not valid for this item

Why can’t I use my enumerator in a static variable?

>Solution :

You can’t just add a variable on its own, it needs to be inside a class. So your code should be:

global using Models

public static class GlobalVariables // Feel free to choose a better name
{
    public static MyEnvironment CurrentEnvironment = MyEnvironment.Development;
}

Also, note there is no "enumerator" here it is just an "enum" or an "enumeration type".

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading