Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created July 7, 2014 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fhernd/34a13831d5e2f08e7f00 to your computer and use it in GitHub Desktop.
Save Fhernd/34a13831d5e2f08e7f00 to your computer and use it in GitHub Desktop.
Demostración del uso del método TryParse de la estructura Int32.
using System;
namespace Articulos.Cap04.Excepciones.Parte2
{
public sealed class UsoInt32TryParse
{
public static void Main()
{
Console.WriteLine ("\nUso 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 ("\nConvertido de `{0}` a {1}.", valor, numero);
}
else
{
if (valor == null)
{
valor = String.Empty;
}
Console.WriteLine ("\nEl intento de convertir `{0}` ha fallado.", valor);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment