|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionAn public enum Rating
{
Awful = 1,
Bad = 2,
Medium = 3,
Good = 4,
Awesome = 5
}
The if(userRating == Rating.Awesome)
{
//Do something
}
If we try to cast an Rating rating = (Rating)6; //does not throw an exception
The way I see it, it is not a good thing, since our SolutionTo overcome these limitations, we can use a regular class to represent an using System.Collections.ObjectModel;
public abstract class EnumBaseType<T> where T : EnumBaseType<T>
{
protected static List<T> enumValues = new List<T>();
public readonly int Key;
public readonly string Value;
public EnumBaseType(int key, string value)
{
Key = key;
Value = value;
enumValues.Add((T)this);
}
protected static ReadOnlyCollection<T> GetBaseValues()
{
return enumValues.AsReadOnly();
}
protected static T GetBaseByKey(int key)
{
foreach (T t in enumValues)
{
if(t.Key == key) return t;
}
return null;
}
public override string ToString()
{
return Value;
}
}
Note that in this class we define a key and a value field. This corresponds to the classical public class Rating : EnumBaseType<Rating>
{
public static readonly Rating Awful = new Rating(1, "Awful");
public static readonly Rating Bad = new Rating(2, "Bad");
public static readonly Rating Medium = new Rating(3, "Medium");
public static readonly Rating Good = new Rating(4, "Good");
public static readonly Rating Awesome = new Rating(5, "Awesome");
public Rating(int key, string value) : base(key, value)
{
}
public static ReadOnlyCollection<Rating> GetValues()
{
return GetBaseValues();
}
public static Rating GetByKey(int key)
{
return GetBaseByKey(key);
}
}
Now it can be used as a regular foreach (Rating rating in Rating.GetValues())
{
Console.Out.WriteLine("Key:{0} Value:{1}", rating.Key, rating.Value);
if (rating == Rating.Awesome)
{
//Do something
}
}
Now let's pretend we are using our public class Rating : EnumBaseType<Rating>
{
public static readonly Rating Awful = new Rating(1, "Awful", false);
public static readonly Rating Bad = new Rating(2, "Bad", false);
public static readonly Rating Medium = new Rating(3, "Medium", false);
public static readonly Rating Good = new Rating(4, "Good", true);
public static readonly Rating Awesome = new Rating(5, "Awesome", true);
public readonly bool MustSee;
public Rating(int key, string value, bool mustSee) : base(key, value)
{
this.MustSee = mustSee;
}
public static ReadOnlyCollection<Rating> GetValues()
{
return GetBaseValues();
}
public static Rating GetByKey(int key)
{
return GetBaseByKey(key);
}
}
Although we added new information to the foreach (Rating rating in Rating.GetValues())
{
if (rating.MustSee)
{
Console.Out.WriteLine("This is a Must See movie!");
}
}
DrawbackThe only drawback of this approach is that the enhanced ConclusionIn this article, we saw how to use classes to simulate the behavior of History
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||