Esempio n. 1
0
        public void Agregar()
        {
            Console.WriteLine("-- Ingrese los datos de persona --");
            Console.Write("Rut: ");
            string rut = Console.ReadLine().Trim();

            Console.Write("Nombre: ");
            string nombre = Console.ReadLine().TrimStart().TrimEnd();

            Console.Write("Edad: ");
            int edad = 0;

            // Forma 1
            //int edad = Int32.Parse(Console.ReadLine().Trim());
            //try
            //{
            //    edad = Convert.ToInt32(Console.ReadLine().Trim());
            //}
            //catch (Exception) {}

            //Persona p = new Persona();
            //p.Rut = rut;
            //p.Nombre = nombre;
            //p.Edad = edad;

            // Forma 2
            string r = Console.ReadLine().Trim();

            if (!int.TryParse(r, out edad))  // TryParse -> bool
            {
                edad = -1;
            }

            if (!Persona.Add(
                    new Persona()
            {
                Rut = rut,
                Nombre = nombre,
                Edad = edad
            }))
            {
                Console.WriteLine("Ocurrió un error al ingresar persona");
            }

            Console.WriteLine($"{nombre} fue agregado exitosamente");
            Console.ReadKey();
        }
 public bool Add(Persona obj)
 {
     return(obj != null && obj.Add());
 }