Problems declaring static enumeration, C #

Hi, I am trying to declare a static enum as follows:

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 get the following error:β€œ The static modifier is not valid for this element. ”I know this is simple, but I don't see a problem. Thanks a lot!

+44
enums c # static
Dec 31 '10 at 3:25
source share
5 answers

Enumerations are types, not variables. Therefore, they are β€œstatic” for each definition; you do not need a keyword.

 public enum ProfileMenuBarTab { MainProfile, Edit, PhotoGallery } 
+97
Dec 31 '10 at 3:30
source share
β€” -

Take out the static .
Enumerations are types, not members; there is no concept of static or non-static enumeration.

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

In addition, you should not create public nested types .

+12
Dec 31 '10 at 3:28
source share

You do not need to define it as static. When an enumerated type is compiled, the C # compiler turns each character into a constant type field. For example, the compiler considers the color enumeration shown earlier as if you wrote code similar to the following:

 internal struct Color : System.Enum { // Below are public constants defining Color 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 value // You cannot write code that references this instance field directly public Int32 value__; } 
+5
May 21 '12 at 2:50 a.m.
source share

You are trying to declare enum declartion static, i.e. a field of type ProfileMenuBarTab . To declare a class (or something else) in the class, leave static.

+1
Dec 31 '10 at 3:29
source share

An enumeration is a type, not a value. The static modifier does not make much sense here.

+1
Dec 31 '10 at 3:30
source share



All Articles