private static int readFilePersons(Person[] a, string f) { int count = 0; StreamReader sr = new StreamReader(f); string lineName; string lineDt; while ((lineName = sr.ReadLine()) !=null) { lineDt = sr.ReadLine(); //string line = Console.ReadLine(); //dt = DateTime.Parse(line); a[count] = new Person(lineName, DateTime.Parse(lineDt)); count ++; } sr.Close(); return count; }
static void Main2(string[] args) { Person p1 = new Person("Alice",new DateTime(1980,1,1),true); p1.Name = "Alice Smith"; p1.Show(); Console.WriteLine("{0} age is more or less {1} ", p1.Name, p1.Age); Person p2 = new Person("Bob the Builder", new DateTime(1992,5,12)); p2.Show(); Console.WriteLine("{0} age is more or less {1} ", p2.Name, p2.Age); Console.WriteLine("O total de pessoas é: {0}", Person.GetNumberOfPersons()); //Person p1 = new Person(); //p1.name = "Alice"; //p1.id = 123; //p1.birthDate = new DateTime(1980, 1, 1); //p1.Show(); //Person p2 = new Person(); //p2.name = "Bob the Builder"; //p2.id = 456; //p2.birthDate = new DateTime(1992,5,12); //p2.Show(); }
static void Main(string[] args) { Person[] malta; malta = new Person[10]; // int mx = 3; // for (int ix = 0; ix < mx; ix++) // malta[ix] = readPerson(); int max = readFilePersons(malta, "pessoas.txt"); malta[max] = new Student("Zé", new DateTime(2001,1,1), 10); Console.WriteLine("Lista de Pessoas"); foreach(Person p in malta) { if (p != null) Console.WriteLine("{0} Idade = {1}", p, p.Age); } int i; Console.Write("Introduza o indice: "); string line = Console.ReadLine(); i = int.Parse(line); // if (malta[i] is Student) pergunta se é do tipo student // Student s = (student) malta[i]; se quisermos forçar o tipo student Student s = malta[i] as Student; // se quisermos que assuma null se n for do tipo student if (s!=null) Console.WriteLine("number = {0}", s.Number); else Console.WriteLine("not student"); //Console.WriteLine("Pessoa : {0}", p.ToString()); }