Thursday, December 28, 2006

Error proof way to convert an enum value into a custom string in C#

Here is a code snippet I ran into the other day:

string strMsgType = "";
switch (messageType)
{
case MessageTypes.GeneralMessage:
strMsgType = "0";
break;
case MessageTypes.TargetMessage1On1:
strMsgType = "1";
break;
case MessageTypes.GroupMessage:
strMsgType = "2";
break;
case MessageTypes.BroadcastMessage:
strMsgType = "3";
break;
case MessageTypes.CallBackMessage:
strMsgType = "4";
break;
case MessageTypes.NoticeMessage:
strMsgType = "5";
break;
case MessageTypes.PaperMessage:
strMsgType = "6";
break;
case MessageTypes.DigitalMessage:
strMsgType = "8";
break;
case MessageTypes.LimitedMessage:
strMsgType = "9";
break;
case MessageTypes.SaleMessage:
strMsgType = "A";
break;
case MessageTypes.AdMessage:
strMsgType = "B";
break;
case MessageTypes.RadioAdMessage:
strMsgType = "C";
break;
case MessageTypes.TvAdMessage:
strMsgType = "D";
break;
case MessageTypes.OtherMessage:
strMsgType = "E";
break;
}


It corresponds to the following enum:

public enum MessageType
{
GeneralMessage = 0,
TargetMessage1On1 = 1,
GroupMessage,
BroadcastMessage,
CallBackMessage,
NoticeMessage,
PaperMessage,
DigitalMessage,
LimitedMessage,
SaleMessage,
AdMessage,
RadioAdMessage,
TvAdMessage,
OtherMessage
}


As you can see, there is a bug in the code as it did not have a case statement for enum value 7. It turns out that, that was as designed, i.e. there should never be an enum value 7, which means the enum declaration is incorrect. Either way, there is a bug in the above code.

It is not uncommon to have specific literal values for the enum strings and trying to correlate those literal values with a switch statement elsewhere is tedious and could be bug prone. Instead a better way to convert an enum value to a custom string would be to do something like this:


string s = Convert.ToInt32(messageType).ToString("X0");

I would love to hear what you think?

Monday, November 20, 2006

Elegant way to parse a string into DateTime instance in C#

I am reviewing some code and came across the following code:

try
{
if (str.Trim() == "") return DateTime.Now;
return (new DateTime(int.Parse(str.Substring(0, 4)), int.Parse(str.Substring(4, 2)), int.Parse(str.Substring(6, 2))));
}
catch
{
return DateTime.Now;
}


Obviously, an exception would be thrown if the string is not in the right format.

So, I looked for a clean way to parse the string into a DateTime instance and came across the following line at (http://www.csharphelp.com/board2/read.html?f=1&i=38662&t=38662)

DateTime.Parse(long.Parse(str).ToString("####/##/##"))

I am not sure if it is more efficient or not compared to the original code snippet I had above, but certainly very elegant, is n't it? We do not have to worry about slicing and joining the string, and instead let the underlying framework class handle all the error checking and if it can't convert it to a valid DateTime instance and then let it throw an exception.

Is not this clean and elegant?

Your comments are welcome.