I am having trouble with enums in C#
I have an enum here:
namespace Project.Shared
{
public enum CostType
{
Dollars,
Percent
}
}
I have an object that is trying to use the enum here:
using Project.Shared;
namespace Project.Shared.FooNamespace
{
public class Foo
{
public int CostType { get; set; }
public Foo()
{
CostType = (int)CostType.Dollars; // <--- error syntax highlighting on 'Dollars'
}
}
}
This results in an error:
‘int’ does not contain a definition for ‘Dollars’ and no extension method ‘Dollars’ accepting a first argument of type ‘int’ could be found (are you missing a using directive or an assembly reference?)
I do not understand why I can’t use my enum there. Can someone help explain it to me?
Because your class
Foohas this definition –public int CostType { get; set; }– which has more local scope than yourenum.Try fully qualifying it with its namespace: