bool isConvertible = false;
int myInt = 0;
isConvertible = int.TryParse(TextBoxD1.Text, out myInt);
The difference between the two is that the first one would throw an exception if the value in your textbox can't be converted, whereas the second one would just return false.
public static class IntegerExtensions
{
public static int ParseInt(this string value, int defaultValue = 0)
{
int parsedValue;
if (int.TryParse(value, out parsedValue))
{
return parsedValue;
}
return defaultValue;
}
public static int? ParseNullableInt(this string value)
{
if (string.IsNullOrEmpty(value))
{
return null;
}
return value.ParseInt();
}
}
And wherever in code just call
int myNumber = someString.ParseInt(); // returns value or 0
int age = someString.ParseInt(18); // with default value 18
int? userId = someString.ParseNullableInt(); // returns value or null
You also may use an extension method, so it will be more readable (although everybody is already used to the regular Parse functions).
public static class StringExtensions
{
/// <summary>
/// Converts a string to int.
/// </summary>
/// <param name="value">The string to convert.</param>
/// <returns>The converted integer.</returns>
public static int ParseToInt32(this string value)
{
return int.Parse(value);
}
/// <summary>
/// Checks whether the value is integer.
/// </summary>
/// <param name="value">The string to check.</param>
/// <param name="result">The out int parameter.</param>
/// <returns>true if the value is an integer; otherwise, false.</returns>
public static bool TryParseToInt32(this string value, out int result)
{
return int.TryParse(value, out result);
}
}
And then you can call it that way:
If you are sure that your string is an integer, like "50".
int num = TextBoxD1.Text.ParseToInt32();
If you are not sure and want to prevent crashes.
int num;
if (TextBoxD1.Text.TryParseToInt32(out num))
{
//The parse was successful, the num has the parsed value.
}
To make it more dynamic, so you can parse it also to double, float, etc., you can make a generic extension.
If you're looking for the long way, just create your one method:
static int convertToInt(string a)
{
int x = 0;
Char[] charArray = a.ToCharArray();
int j = charArray.Length;
for (int i = 0; i < charArray.Length; i++)
{
j--;
int s = (int)Math.Pow(10, j);
x += ((int)Char.GetNumericValue(charArray[i]) * s);
}
return x;
}
this may help you ;D
Another way would be:
The difference between the two is that the first one would throw an exception if the value in your textbox can't be converted, whereas the second one would just return false.
You can write your own extesion method
And wherever in code just call
In this concrete case
You also may use an extension method, so it will be more readable (although everybody is already used to the regular Parse functions).
And then you can call it that way:
If you are sure that your string is an integer, like "50".
If you are not sure and want to prevent crashes.
To make it more dynamic, so you can parse it also to double, float, etc., you can make a generic extension.
You can convert string to an integer value with the help of parse method.
Eg:
If you're looking for the long way, just create your one method: