below extension methods converts an object/double/string to integer and if its not convertable then returns 0.
public static int ToInt(this object source)
{
int val = 0;
if (source != null)
{
string str = Convert.ToString(source);
if (!string.IsNullOrEmpty(str))
{
int.TryParse(str, out val);
if (val <= 0)
{
try
{
val = (int)Convert.ToDouble(str);
}
catch (Exception)
{
}
}
}
}
return val;
}
9:26 AM
Rajesh Rolen
0 comments:
Post a Comment