Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using System;
  2.  
  3. namespace Articulos.Cap04.Excepciones.Parte2
  4. {
  5.     public sealed class UsoInt32TryParse
  6.     {
  7.         public static void Main()
  8.         {
  9.             Console.WriteLine ("\nUso del método TryParse de Int32.");
  10.             IntentoDeConversion (null);
  11.             IntentoDeConversion ("160519");
  12.             IntentoDeConversion ("9432.0");
  13.             IntentoDeConversion ("16,667");
  14.             IntentoDeConversion ("   -322   ");
  15.             IntentoDeConversion ("+4302");
  16.             IntentoDeConversion ("(100);");
  17.             IntentoDeConversion ("01FA");
  18.         }
  19.        
  20.         private static void IntentoDeConversion (string valor)
  21.         {
  22.             int numero;
  23.            
  24.             // true: si la conversión fue satisfactoria.
  25.             // false: si la conversión falló:
  26.             bool convirtio = Int32.TryParse (valor, out numero);
  27.            
  28.             if (convirtio)
  29.             {
  30.                 Console.WriteLine ("\nConvertido de `{0}` a {1}.", valor, numero);
  31.             }
  32.             else
  33.             {
  34.                 if (valor == null)
  35.                 {
  36.                      valor = String.Empty;
  37.                 }
  38.                
  39.                 Console.WriteLine ("\nEl intento de convertir `{0}` ha fallado.", valor);
  40.             }
  41.         }
  42.     }
  43. }