enum question
-
Hi, is there a way to use an enum as condition in an if statement. Say for example can I change the following code and use enum instead /thx: string houseType = string.Empty; //is set to different housetypes ... ... if(housType.Equals("detached house")) ..do something else if(houseType.Equals("manor")) ..do something else
-
Hi, is there a way to use an enum as condition in an if statement. Say for example can I change the following code and use enum instead /thx: string houseType = string.Empty; //is set to different housetypes ... ... if(housType.Equals("detached house")) ..do something else if(houseType.Equals("manor")) ..do something else
Sure:
enum HouseType {UNKNOWN, DETACHED, ... }
HouseType myHouse=HouseType.UNKNOWN;
...
if (myHouse==HouseType.DETACHED) ...
...Now if you want enum values that are not valid identifiers, you need some special trick, e.g. add Attributes to each enum value, and retrieve them using reflection. :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
Sure:
enum HouseType {UNKNOWN, DETACHED, ... }
HouseType myHouse=HouseType.UNKNOWN;
...
if (myHouse==HouseType.DETACHED) ...
...Now if you want enum values that are not valid identifiers, you need some special trick, e.g. add Attributes to each enum value, and retrieve them using reflection. :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
Sure:
enum HouseType {UNKNOWN, DETACHED, ... }
HouseType myHouse=HouseType.UNKNOWN;
...
if (myHouse==HouseType.DETACHED) ...
...Now if you want enum values that are not valid identifiers, you need some special trick, e.g. add Attributes to each enum value, and retrieve them using reflection. :)
Luc Pattyn [My Articles] [Forum Guidelines]
Or use static class, like
class HouseType
{
HouseType(string){...}string desc; public string Description { get {return desc;}}
}
static class HouseTypes
{
static HouseTypes()
{
type1 = new HouseType("House of type1");
}
public HouseType Type1 { get {return type1}}
}...
if(myHouse.HouseType == HouseTypes.Type1)...
It should have better performance then using attributes but it consumes some extra memory.
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe