DatType TryPraser Check to avoid Format Exceptsiona in runtime.

muralikrishnasurap-12704
Posted by muralikrishnasurap-12704 under C# category on | Points: 40 | Views : 462
Hi Here I want to explain how to use datatyps int.parse(value) method to avoid Format exceptions while converting the values at run time.


 class ConversionCheck
{
public string name = "dotnetFunda";
public int iresult;
public decimal dresult;
public void ConvertingDataWithTryParser()
{
if (int.TryParse(name, out iresult))
Console.WriteLine("value after Convertsion" + iresult);
else
Console.WriteLine("Failed to convert to int");

if (decimal.TryParse(name, out dresult))
Console.WriteLine("value after Convertsion" + dresult);
else
Console.WriteLine("Failed to convert to decimal");


}
public void ConvertingDataWithOutTryParser()
{

Convert.ToInt32(name);//Format Exception
}
}
}

Comments or Responses

Login to post response