The VB.NET to C# converter Instant C# makes a static class available to mimic the robustness of the VB cast macro "CInt":
internal static int CInt(object oValue)
{
try
{
if (oValue == null || System.Convert.IsDBNull(oValue))
//default null to 0 (identical to the VB.NET cast):
return 0;
double dValue;
if (double.TryParse(oValue.ToString().Trim(),
NumberStyles.Any, CultureInfo.CurrentCulture, out dValue))
if (dValue <= int.MaxValue && dValue >= int.MinValue)
return (int)(System.Math.Round(dValue));
else
return 0;
else
return 0;
}
catch
{
return 0;
}
}