Reputation: 21206
I know I can declare an enum
directly in the Namespace so everyone can access it being in the same namespace.
But can I declare it in a class to access from other class:
public class Class1
{
public enum Mode { Selected, New }
}
public class Class2
{
var class1 = new ();
// Set the Mode
class1.Mode = Mode.Selected;
}
Is it possible to use enums
without declaring it in the namespace?
Upvotes: 17
Views: 84312
Reputation: 433
The question ain't very clear and each answer seem to approach on a subjective way so here is my take:
enum
we can declare it on the same class, it will be accessible on all person instances.namespace MyApp.Models
{
public class Person
{
public AgeGroup ageGroup;
public enum AgeGroup { Baby, Child, Teen, Adult, Senior}
}
}
Declaration:
using MyApp.Models;
(...)
Person p = new();
p.ageGroup = Person.AgeGroup.Baby;
But you can see that it gets a bit verbose with Person.AgeGroup.Baby
.
enum
in the same namespace as the class we wanna use, if unique to the class preferably in the same file, just below the class but that's absolutely optional.This method lets the enum be used all over the namespace where its declared.
namespace MyApp.Models
{
public class Person
{
public AgeGroup ageGroup;
}
public enum AgeGroup { Baby, Child, Teen, Adult, Senior}
}
Declaration:
using MyApp.Models;
(...)
Person p = new();
p.ageGroup = AgeGroup.Baby;
Note that the namespace is the same as the Person class so no aditional namespace is required.
enum
is used "everywhere" on your project(s) you can declare it on a more accessible namespace, for example your main project namespace:namespace MyApp
{
//this could be your "App.cs" file with the enum declared after the class
//or just a new "SomeEnums.cs" file
public enum Mode {A, B}
}
Declaration:
namespace MyApp.ViewModels
{
public class Test
{
Mode mode = Mode.A;
}
}
Note that in the previous example I declared on the MyApp namespace but I used on child/nested namespace MyApp.ViewModels.
namespace MyApp.Helpers
{
public enum Mode { A, B}
}
Declaration:
using MyApp.Helpers;
namespace MyApp
{
public class Test
{
Mode mode = Mode.A;
}
}
Upvotes: 0
Reputation: 14515
I ended up solving my issue by changing it to a namespace accessor, found by utilizing Intellisense. I thought the enum was in the class, not just the namespace. If it was in the class, I would recommend moving it out of the class.
namespace ABC.XYZ.Contracts
{
public class ChangeOrder : BaseEntity, IAuditable
{
...
}
public enum ContractorSignatureType
{
A,
B,
V
}
}
ContractorSignatureType = Contracts.ContractorSignatureType.B,
Upvotes: 1
Reputation: 2710
Aaron's answer is very nice but I believe there is a much better way to do this:
public static class class1
{
public void Run()
{
class2.Mode mode = class2.Mode.Selected;
if (mode == class2.Mode.Selected)
{
// Do something crazy here...
}
}
}
public static class class2
{
public enum Mode
{
Selected,
New
}
}
No point over complicating this. It is a simple task.
All the Best
Chris.
Upvotes: 10
Reputation: 24713
If you are trying to do what is described below it will not work...
public class MyClass1
{
private enum Mode { New, Selected };
public Mode ModeProperty { get; set; }
}
public class MyClass2
{
public MyClass2()
{
var myClass1 = new MyClass1();
//this will not work due to protection level
myClass1.ModeProperty = MyClass1.Mode.
}
}
What you could do however is below, which will work...
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;
}
}
Upvotes: 4
Reputation: 13947
You can declare an enum outside of a class:
namespace MyNamespace
{
public enum MyEnum
{
Entry1,
Entry2,
}
}
And then you can add using MyNamespace;
where it needs to be used.
Upvotes: 23
Reputation: 169008
Yes:
class2.Mode = class2.Mode.Selected
But note that you can't have a nested type defined that has the same name as one of the outer class' members, so this code will not compile. Either the enum or the property will need to be named something else. Your class name and variable name conflict too, making this a bit more complex.
To make this a more generic answer, if you have this:
public class Foo
{
public SomeEnum SomeProperty { get; set; }
public enum SomeEnum {
Hello, World
}
}
Then this code will assign an enum value to the property:
Foo foo = new Foo();
foo.SomeProperty = Foo.SomeEnum.Hello;
Upvotes: 5