954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Enum issue

Hi all,
The following does work, but not as I expected.
If I use an enum I normally don't have to prefix it with a class name.
Here it seems the only option. Can anyone tell me why?

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            MyEnums E = new MyEnums();
            // Why do I have to use this instead of E.Pets
            // or simply Pets.cat;?
            // Looks like some static behaviour, but I don't understand why...
            E.P = MyEnums.Pets.cat;
        }
    }

    class MyEnums
    {
        public enum Pets
        {
            cat,dog,tiger
        }

        public Pets P;
    }
}
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

You declared the Enum inside of a class. You can declare enums directly in the namespace like you're accustomed to:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmEnums : Form
  {
    public frmEnums()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      GlobalEnum ge = GlobalEnum.Value1;
      OrganizationalClass.ClassEnum ce = OrganizationalClass.ClassEnum.Value1;
    }
  }

  public enum GlobalEnum
  {
    Value1,
    Value2
  }

  public static class OrganizationalClass
  {
    public enum ClassEnum
    {
      Value1,
      Value2
    }
  }
}
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

Did not know you could do that. Thanks Scott :)

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: