C#: What `using` statement do I need here?
I have a project in MS Visual Studio 2008 Pro. I'm new to the environment and the language, so forgive a noobish question.
I have a type ControlCode
:
namespace ShipAILab
{
public abstract class ControlUnit {
public enum ControlCode {
NoAction = 0x00,
MoveLeft = 0x01,
MoveUp = 0x02,
MoveRight = 0x04,
MoveDown = 0x08,
开发者_如何学C Fire = 0x10,
}
}
}
I want this to be accessible from another class, BoardUtils
, which is in the same ShipAILab
namespace:
public static IList<ControlUnit.ControlCode> pathToPoint(IDictionary<CompPoint, int> dist, CompPoint destination) {
ControlUnit.ControlCode code = ControlUnit.ControlCode.MoveLeft; // works
ControlCode c2 = ControlCode.MoveDown; // how do I get this to work?
}
Why doesn't this work automatically, by virtue of sharing a namespace? Do I need a using
statement? Can I "typedef" it like in C to rename ControlUnit.ControlCode
to something more concise?
Your enumeration is inside the ControlUnit
class.
If you want it to be accessible without typing the class name, move the enumeration outside the class.
Move the enum to outside the class.
namespace ShipAILab
{
public enum ControlCode {
NoAction = 0x00,
MoveLeft = 0x01,
MoveUp = 0x02,
MoveRight = 0x04,
MoveDown = 0x08,
Fire = 0x10,
}
public abstract class ControlUnit {
}
}
You shouldn't need a using statement if they're in the same namespace.
Are they in the same dll (same project under VS2008). If not, then you'll need to add a reference from the pathToPoint
dll to the one that declares ControlUnit
. You can do this by finding the project in the solution explorer, right clicking on it, and picking 'Add Reference...'
Update
If the code as shown does actually compile, and you want to make it so you don't have to type ControlUnit.XYZ
all over the place, then you need to move the declaration of the enum outside the ControlUnit
class
精彩评论