C#, Enums and Strings oh my!
May 07
I find myself having to lookup the same snippets of code a lot. You know, those insignificant lines of code you “never” use.
Recently I have had to look this up twice, and hopefully by placing it here, I will never need to again (knock on wood). So again, without further ado… drum roll please…
enum CcType { Visa, MasterCard, Discover, Amex, Solo, Maestro }
CcType ccType = CcType.Visa;
string cardType = ccType.ToString();
// This will make cardType = "Visa"
// This is easy to remember, because well you are telling it to turn visa to a string.
// The next part I have to look up each time
ccType = (CcType)Enum.Parse(typeof(ccType), "MasterCard");
// There you go, the simple one line string to enum solution.
I hope this saves some Googling… for you and me!
