Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

troubles declaring static enum, C#

Tags:

c#

enums

static

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!

like image 414
RayLoveless Avatar asked Dec 31 '10 03:12

RayLoveless


People also ask

Can enums be static?

An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).

What is static enum in C?

static enum direction {UP,DOWN,RIGHT,LEFT}; This declares an enumerated type with the tag name directions , and also defines a number of enumeration constants and their values. The static storage-class is meaningless here, because you are not defining (allocating storage) for an object.

Which is the correct way of declaring an enum?

The keyword 'enum' is used to declare new enumeration types in C and C++. Following is an example of enum declaration. // The name of enumeration is "flag" and the constant // are the values of the flag.

Is enum by default static?

Every enum constant is static.


2 Answers

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

public enum ProfileMenuBarTab { MainProfile, Edit, PhotoGallery } 
like image 156
magnattic Avatar answered Sep 21 '22 07:09

magnattic


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.

like image 22
SLaks Avatar answered Sep 18 '22 07:09

SLaks