using System;
namespace Articulos.Cap04 .Excepciones .Parte2
{
public sealed class UsoInt32TryParse
{
public static void Main( )
{
Console.WriteLine ( "\n Uso del método TryParse de Int32." ) ;
IntentoDeConversion ( null ) ;
IntentoDeConversion ( "160519" ) ;
IntentoDeConversion ( "9432.0" ) ;
IntentoDeConversion ( "16,667" ) ;
IntentoDeConversion ( " -322 " ) ;
IntentoDeConversion ( "+4302" ) ;
IntentoDeConversion ( "(100);" ) ;
IntentoDeConversion ( "01FA" ) ;
}
private static void IntentoDeConversion ( string valor)
{
int numero;
// true: si la conversión fue satisfactoria.
// false: si la conversión falló:
bool convirtio = Int32.TryParse ( valor, out numero) ;
if ( convirtio)
{
Console.WriteLine ( "\n Convertido de `{0}` a {1}." , valor, numero) ;
}
else
{
if ( valor == null )
{
valor = String.Empty ;
}
Console.WriteLine ( "\n El intento de convertir `{0}` ha fallado." , valor) ;
}
}
}
}
dXNpbmcgU3lzdGVtOwoKbmFtZXNwYWNlIEFydGljdWxvcy5DYXAwNC5FeGNlcGNpb25lcy5QYXJ0ZTIKewogICAgcHVibGljIHNlYWxlZCBjbGFzcyBVc29JbnQzMlRyeVBhcnNlCiAgICB7CiAgICAgICAgcHVibGljIHN0YXRpYyB2b2lkIE1haW4oKQogICAgICAgIHsKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUgKCJcblVzbyBkZWwgbcOpdG9kbyBUcnlQYXJzZSBkZSBJbnQzMi4iKTsKICAgICAgICAgICAgSW50ZW50b0RlQ29udmVyc2lvbiAobnVsbCk7CiAgICAgICAgICAgIEludGVudG9EZUNvbnZlcnNpb24gKCIxNjA1MTkiKTsKICAgICAgICAgICAgSW50ZW50b0RlQ29udmVyc2lvbiAoIjk0MzIuMCIpOwogICAgICAgICAgICBJbnRlbnRvRGVDb252ZXJzaW9uICgiMTYsNjY3Iik7CiAgICAgICAgICAgIEludGVudG9EZUNvbnZlcnNpb24gKCIgICAtMzIyICAgIik7CiAgICAgICAgICAgIEludGVudG9EZUNvbnZlcnNpb24gKCIrNDMwMiIpOwogICAgICAgICAgICBJbnRlbnRvRGVDb252ZXJzaW9uICgiKDEwMCk7Iik7CiAgICAgICAgICAgIEludGVudG9EZUNvbnZlcnNpb24gKCIwMUZBIik7CiAgICAgICAgfQogICAgICAgIAogICAgICAgIHByaXZhdGUgc3RhdGljIHZvaWQgSW50ZW50b0RlQ29udmVyc2lvbiAoc3RyaW5nIHZhbG9yKQogICAgICAgIHsKICAgICAgICAgICAgaW50IG51bWVybzsKICAgICAgICAgICAgCiAgICAgICAgICAgIC8vIHRydWU6IHNpIGxhIGNvbnZlcnNpw7NuIGZ1ZSBzYXRpc2ZhY3RvcmlhLgogICAgICAgICAgICAvLyBmYWxzZTogc2kgbGEgY29udmVyc2nDs24gZmFsbMOzOgogICAgICAgICAgICBib29sIGNvbnZpcnRpbyA9IEludDMyLlRyeVBhcnNlICh2YWxvciwgb3V0IG51bWVybyk7CiAgICAgICAgICAgIAogICAgICAgICAgICBpZiAoY29udmlydGlvKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSAoIlxuQ29udmVydGlkbyBkZSBgezB9YCBhIHsxfS4iLCB2YWxvciwgbnVtZXJvKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGlmICh2YWxvciA9PSBudWxsKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICB2YWxvciA9IFN0cmluZy5FbXB0eTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUgKCJcbkVsIGludGVudG8gZGUgY29udmVydGlyIGB7MH1gIGhhIGZhbGxhZG8uIiwgdmFsb3IpOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9