user1054625
user1054625

Reputation: 471

How to access ENUM which is declared in a separate class - C#

I have the following code:

class EmployeeFactory
{
        public enum EmployeeType
        {
                ManagerType,
                ProgrammerType,
                DBAType
        }
}

I want to access this in MAIN class (Program). I have written the following code. IT WORKS. But I want to know how I can access the ENUM without instantiating the class -- Means ENUM is like a static variable (Class Level Variable) ? Any help ?

class Program
{
        static void Main(string[] args)
        {
                Console.WriteLine(EmployeeFactory.EmployeeType.ProgrammerType);  // WORKS WELL
        }
}

or do I need to write it this way?

EmployeeFactory ef = new EmployeeFactory();
ef.EmployeeType.ProgrammerType

Upvotes: 4

Views: 12659

Answers (3)

N_A
N_A

Reputation: 19897

You can access it simply using the class.

EmployeeFactory.EmployeeType.ProgrammerType

The enum is part of the class, not part of a class instance.

Upvotes: 5

Glory Raj
Glory Raj

Reputation: 17701

try something like this ...

    public interface IEnums
    {
        public enum Mode { New, Selected };
    }

    public class MyClass1
    {
        public IEnums.Mode ModeProperty { get; set; }
    }

    public class MyClass2
    {
        public MyClass2()
        {
            var myClass1 = new MyClass1();

            //this will work
            myClass1.ModeProperty = IEnums.Mode.New;
        }
    }

or you can directly access like this....

 EmployeeFactory.EmployeeType.ProgrammerType 

i hope it will helps you

Upvotes: -2

Adam Rackis
Adam Rackis

Reputation: 83358

But I want to know how I can access the ENUM without instantiating the class

The original way you're accessing this enum

Console.WriteLine(EmployeeFactory.EmployeeType.ProgrammerType);

already accomplishes that; you are accessing the enum without instantiating the class.

Upvotes: 1

Related Questions