Alex Lungu
Alex Lungu

Reputation: 1114

Encapsulate enum

I´m trying to understand how to use enumerations in c#. I already googled it but I can´t figure out how to encapsulate an enumeration.

I have a class which contains an enum attribute. How do I encapsulate this attribute so I can use it outside the class?

private enum cost { Price, Diff };

Upvotes: 0

Views: 1758

Answers (3)

stann1
stann1

Reputation: 635

You just treat the enum as a normal class object.

So lets say you have this enum:

public enum CoffeType
{
    regular,
    decaf
}

and you have a class:

public class CoffeeOrder
{
    public string CustomerName {get;set}
    public CoffeeType CoffeeType {get;set;}
    // ..other properties 
}

and you see that the enum is encapsulated just like a normal class property...

Upvotes: 0

tichra
tichra

Reputation: 553

You could move the enum declaration outside of the class as below, then this can be used from all over the project as cost cos = cost.Price

public enum cost 
    {
        Price, Diff
    }

    public class ExampleClass
    {
    }

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186708

You have to declare the enum:

  public enum Cost { 
    Price, 
    Diff 
  };

then use it

  public class MyClass {
    // property of "Cost" type
    public Cost MyCost {
      get;
      set; 
    }
    ...
  }

Upvotes: 2

Related Questions