Created
July 7, 2014 21:22
-
-
Save Fhernd/34a13831d5e2f08e7f00 to your computer and use it in GitHub Desktop.
Demostración del uso del método TryParse de la estructura Int32.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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