This was a bad weekend for the car. Four new tires and a tuneup.
There went $850. Ouch.
Comments from an alien-human hybrid
This was a bad weekend for the car. Four new tires and a tuneup.
There went $850. Ouch.
That last post was brought to you with Windows Live Writer and a nifty Source Code Formatter.
Now I just need to figure out how to make my chosen template look decent with the main text window wider, so the source code doesn't look bad.
public enum SUTypeAmong other things I use the enum to generate names for items. In C++, I would probably write a function to convert the enum to the string I wanted, but this isn't c++, and I wanted to try something different.
{
INVALID,
POTION,
OIL,
DETONATION,
OTHER,
}
public enum SUType
{
[Description("Unknown type")]
INVALID,
[Description("Potion")]
POTION,
[Description("Oil")]
OIL,
[Description("Detonation")]
DETONATION,
[Description("Single Use Item")]
OTHER,
}
Next I added this public static method in a helper class:
public class ItemHelper
{
/// <summary>
/// Returns the DescriptionAttirbute for an
/// enum.
/// </summary>
/// <param name="nType">Any enum</param>
/// <returns>DescriptionAttribute for that
/// enum, if it exists</returns>
public static string GetTypeDescription(Enum nType)
{
Type oSystype = nType.GetType();
string strName = System.Enum.GetName(oSystype, nType);
FieldInfo oFieldInfo = oSystype.GetField(strName);
object[] rgObjs =
oFieldInfo.GetCustomAttributes(
typeof(DescriptionAttribute), false);
foreach (object obj in rgObjs)
{
DescriptionAttribute oDesc = obj as
DescriptionAttribute;
if (oDesc != null)
{
return oDesc.Description;
}
}
return "Unknown";
}
}
You use it like this:
string strType = ItemHelper.GetTypeDescription(m_nType);
This isn't perfect. For one, if you have multiple description attributes you aren't guaranteed to find a particular one first. It also doesn't handle localization at all. But this works for what I need right now.
It ain't pretty, and it's not as nice as source control integration, but it does work.