troubles declaring static enum, C#

C#StaticEnums

C# Problem Overview


Hi I'm trying to declar a static enum like so:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Lds.CM.MyApp.Controllers
{
    public class MenuBarsController : Controller
    {
        // Menu Bar enums
        public static enum ProfileMenuBarTab { MainProfile, Edit, photoGallery }

        public ActionResult cpTopMenuBar(string tabSelected)
        {
            ...            

" But I'm getting the following error: "The modifier 'static' is not valid for this item." I know it's something simple but I can't seem to see the problem. Much thanks!

C# Solutions


Solution 1 - C#

Enums are types, not variables. Therefore they are 'static' per definition, you dont need the keyword.

public enum ProfileMenuBarTab { MainProfile, Edit, PhotoGallery }

Solution 2 - C#

Take out static.
Enums are types, not members; there is no concept of a static or non-static enum.

You may be trying to make a static field of your type, but that has nothing to do with the type declaration.
(Although you probably shouldn't be making a static field)

Also, you should not make public nested types.

Solution 3 - C#

You don't need to define it as static.When an enumerated type is compiled, the C# compiler turns each symbol into a constant field of the type . For example, the compiler treats the Color enumeration shown earlier as if you had written code similar to the following:

internal struct Color : System.Enum {
            // Below are public constants defining Color's symbols and values
            public const Color White  = (Color) 0;
            public const Color Red    = (Color) 1;
            public const Color Green  = (Color) 2;
            public const Color Blue   = (Color) 3;
            public const Color Orange = (Color) 4;
            // Below is a public instance field containing a Color variable's value
            // You cannot write code that references this instance field directly
            public Int32 value__;
}

Solution 4 - C#

An enum is a type, not a value. The modifier static doesn't make much sense there.

Solution 5 - C#

You are trying to make an enum declartion static, ie a field of the type ProfileMenuBarTab. To declare a class (or whatever) in a class, leave the static out.

Solution 6 - C#

Types in .Net can be either value types or reference types.

Value types -> enums, structs and built-in values types(bool, byte, short, int, long, sbyte, ushort, uint, ulong, char, double, decimal)

Reference types -> classes, interfaces, delegates, dynamic and strings

So, as you can see enums are types(like classes and structs, etc). more precisely they are value types. An important point about value types is that you should be able to create instances from them. For example, What is its benefit of int that is a struct (value type) if you can't create an instance of that for storing 2, 3 or any number in it?!

This is the general rule -> you cannot create custom value types (enums and structs) with the static modifier.

Some points:

  • If you write your enums or structs directly in a namespace they can not be marked as private or protected just like other types. They can be just public or internal just like other types.

  • If you write your enums or structs directly in a class you can mark them as private or protected too, as you can mark them as internal and public. class for inner types is like a namespace for types except you can mark inner types private or public too.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRayLovelessView Question on Stackoverflow
Solution 1 - C#magnatticView Answer on Stackoverflow
Solution 2 - C#SLaksView Answer on Stackoverflow
Solution 3 - C#TarikView Answer on Stackoverflow
Solution 4 - C#Brian ClapperView Answer on Stackoverflow
Solution 5 - C#FemarefView Answer on Stackoverflow
Solution 6 - C#user6755993View Answer on Stackoverflow