Reputation: 471
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
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
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
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