TryParse
Written by: maffelu , 2009-02-18 08:22:00
public static bool TryParse(string s, out int result)
Console.Write("Enter an integer value: ");
string myNumber = Console.ReadLine();
int myInt;
bool myCheck;
myCheck = int.TryParse(myNumber, out myInt);
if(myCheck == false)
{
Console.WriteLine("Error: {0} is not an integer value", myNumber);
}
else
{
Console.WriteLine("Congratulations, {0} is an integer value!", myInt);
}
Console.Read();
Console.Write("Enter an integer value: ");
string myNumber = Console.ReadLine();
int myInt;
bool myCheck;
try
{
myInt = int.Parse(myNumber);
}
catch (FormatException ex1)
{
Console.WriteLine("Error: {0}", ex1);
myInt = 0;
}
catch (OverflowException ex2)
{
Console.WriteLine("Error: {0}", ex2);
myInt = 0;
}
Console.WriteLine("Converted string: {0}", myInt);
Console.Read();
There are no comments on this article.
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only
Maffelu
Creator and admin of MorkaLork.com.
Started programming in HTML back when frames and tables was the way to design a page, moved on to Pascal/Delphi, PHP, javascript/jQuery, VB.NET/C#, Java and C++.
Currently studies .NET (in general) focusing on ASP.NET.