I heard that nesting of enum
is not possible in C#. Then how can convert the following structure to a class hierarchy or something else. So I want the class to act as an enum
nested classes and const fields
class Cat1
{
public const int Public = 1;
public class Private
{
public const int Abc = 2;
public const int Mno = 3;
public const int Pqr = 4;
}
}
public class Cat1
{
public enum Publicpart
{
Xyz
}
private enum Privatepart
{
Abc, Mno, Pqr
}
}
then you can call it like this
Cat1.Publicpart.Xyz
or if you have private acces
Cat1.Privatepart.Abc
you can use the hirerchy as class structure , which every class has a property of its own of enum
You should rethink if you want to solve this problems via enums because the first enum category represents to me some kind of "visibility" concept while the second category is only valid of instances with visibility "public".
What about solving your issue with something like this:
public enum Visibility
{
Public,
Private
}
public abstract class VisibilityState
{
public Visibility Visibility { get; private set; }
protected VisibilityState(Visibility visibility)
{
Visibility = visibility;
}
}
public class PublicVisibilityState : VisibilityState
{
public PublicVisibilityState() : base(Visibility.Public) { }
}
public class PrivateVisibilityState : VisibilityState
{
public PrivateVisibilityState() : base(Visibility.Private) { }
public OtherEnum OtherEnumState { get; set; }
}
public enum OtherEnum
{
Abc, Mno, Pqr
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With