I’m using C# in VS2005. I have a class library that contains several enums common to a number of different projects. When accessing one of these enums I have to specify the whole namespace path to the enum even though I have declared a ‘using’ directive to the namespace that contains the enum.
For example I have the following enum:
namespace Company.General.Project1
{
public static class Rainbow
{
[Flags]
public enum Colours
{
Red,
Blue,
Orange
}
}
}
Then in another project I have:
using Company.General.Project1;
namespace Company.SpecialProject.Processing
{
public class MixingPallette
{
int myValue = Company.General.Project1.Colours.Red;
}
}
Even though I have the ‘Using’ directive referencing the project that contains the class of the enum, I still have to write the enum longhand.
Why can’t I do the following…
using Company.General.Project1;
namespace Company.SpecialProject.Processing
{
public class MixingPallette
{
int myValue = Colours.Red;
}
}
Your enum isn’t just in a namespace – it’s a nested type. In fact, your sample “working” code wouldn’t work, it would have to be
(Not only do you need to include the
Rainbowpart, but there’s also no implicit conversion from an enum to int.)Make your enum a top-level type:
Then you will be able to write:
(Note that to use
[Flags]effectively, you should be assigning values explicitly, e.g. 1, 2, 4, 8…)EDIT: I’ve been assuming you really do want to be able to use
Colours.Redetc. You can keep your current structure, using a nested type, and just write:Unless you have a particular reason to make the enum nested, however, I wouldn’t.