I have a class that returns an object type to a variable. The variable Must know what the real type is when operations are performed on it:
public object Data
{
get
{
switch (CriteriaID)
{
case (int)matrix2.enums.NodeTypeEnums.Enums.MultiLineText:
return (string)_Data;
case (int)matrix2.enums.NodeTypeEnums.Enums.SingleLineText:
return (string)_Data;
case (int)matrix2.enums.NodeTypeEnums.Enums.Number:
int temp = 0;
return int.TryParse((string)_Data, out temp) ? (int?)temp : null;
case (int)matrix2.enums.NodeTypeEnums.Enums.Price:
decimal temp1 = 0;
return decimal.TryParse((string)_Data, out temp1) ? (decimal?)temp1 : null;
case (int)matrix2.enums.NodeTypeEnums.Enums.PullDown:
return (string)_Data;
case (int)matrix2.enums.NodeTypeEnums.Enums.Checkbox:
bool temp2 = false;
return bool.TryParse((string)_Data, out temp2) ? (bool?)temp2 : null;
case (int)matrix2.enums.NodeTypeEnums.Enums.Date:
DateTime temp3 = DateTime.MinValue;
return DateTime.TryParse((string)_Data, out temp3) ? ((DateTime?)temp3).Value.ToString("MM/dd/yyyy") : null;
case (int)matrix2.enums.NodeTypeEnums.Enums.Link:
return (string)_Data;
case (int)matrix2.enums.NodeTypeEnums.Enums.Image:
return (string)_Data;
default:
return (string)_Data;
}
}
set
{
_Data = value;
}
}
The data property is used like this:
temp.Count() > 0 ? temp.FirstOrDefault().Data : " "
Using it like this works but I am not sure if this is the best implementation and/or the most efficient. Is their a better way to do this?