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;
    }
}

Recommended Answers

All 2 Replies

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
    }
  }
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.