|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThose of us who write ASP.NET for living often come across the problem of wanting to use public enum SeatType
{
Window=1,
Aisle=2
}
public class Registration
{
...
SeatType wantsSeat;
...
}
Back in your ASP.NET page, you may loop over all the foreach (string s in Enum.GetNames(typeof(SeatType)))
{
string name = s;
SeatType t = (SeatType) Enum.Parse(typeof(SeatType),s);
int val = (int) t;
//do something with name and val like
//add them to an <option> tag
}
This code is a bit ugly, but it's worth keeping due to the fact that we can have methods like: public SeatType GetPassengerSeatType(Passenger p) {
return p.Seat;
}
The real issue arises when we add a new seat type to our public enum SeatType
{
Window=1,
Aisle=2,
Anything Except Seat Near Bathroom
}
Since "Anything Except Seat Near Bathroom" isn't a valid identifier, I've seen several people make use of attributes to do something like this: public enum SeatType
{
[Description("Window")] Window=1,
[Description("Aisle")] Aisle=2,
[Description("Anything Except Seat" +
" Near Bathroom")] AnythingExceptSeatNearBathroom
}
public static string GetEnumDescription(Enum value)
{
FieldInfo fi= value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute), false);
return (attributes.Length>0) ?
attributes[0].Description :
value.ToString();
}
This code works, but it's not really good object oriented design: we're asking another class to retrieve information about our With .NET 2.0 and generics, we are able to have a very clean solution to the problem. Using the codeI created a simple generic abstract class called Before explaining how the public class SeatType : DescriptiveEnum<SeatType,int>
{
public static readonly SeatType Window =
new SeatType("Window Seat",1);
public static readonly SeatType Aisle =
new SeatType("Aisle Seat", 2);
public static readonly SeatType
AnythingExceptSeatNearBathroom =
new SeatType("Anything Except Seat Near Bathroom", 3);
private SeatType(string desc, int code) : base(desc,code)
{
}
}
Only three things are necessary here:
So after doing the above, we have a data type that looks and feels just like a built-in SeatType c = SeatType.Aisle;
string desc = SeatType.AnythingExceptSeatNearBathroom.Description;
Additionally, our base class defines some other useful methods: SeatType c;
int seatkind = GetSeatTypeFromDatabaseSomeWhere();
c = SeatType.GetEnumFromCode(seatkind);
SeatType[] allSeatTypes = SeatType.GetEnumMembers();
Finally, with generics, the base class is able to define SeatType.GetEnumFromCode(seatkind);
like we did above. You can actually just do: SeatType c = (SeatType) GetSeatTypeFromDatabaseSomeWhere();
Likewise, you can call: int x = (int) SeatType.Aisle
if you want. Although, generally, I would call the And that's all there is to it. Generics is a beautiful thing. History
|
||||||||||||||||||||||