class Personne
{
private char sexe;
private string nomPre, taille, poids;
private int numero;
public Personne(string nomPre, char sexe, string taille, string poids, int numero)
{
this.nomPre = nomPre;
this.sexe = sexe;
this.taille = taille;
this.poids = poids;
this.numero = numero;
}
public Personne(string ligneLue)
{
string nomPre = ligneLue.Substring(0,30).Trim();
char sexe = char.Parse(ligneLue.Substring(31,7).Trim());
string taille = ligneLue.Substring(38,14).Trim();
string poids = ligneLue.Substring(53,12).Trim();
int numero = int.Parse(ligneLue.Substring(66).Trim());
}
public string GetNomPre()
{
return nomPre;
}
public char GetSexe()
{
return sexe;
}
public string GetTaille()
{
return taille;
}
public string GetPoids()
{
return poids;
}
public int GetNumero()
{
return numero;
}
//accesseurs
public void SetNomPre(string nomPre)
{
this.nomPre = nomPre;
}
public void SetSexe(char sexe)
{
this.sexe = sexe;
}
public void SetTaille(string taille)
{
this.taille = taille;
}
public void SetPoids(string poids)
{
this.poids = poids;
}
public void SetNumero(int numero)
{
this.numero = numero;
}
//modificateurs
public void Afficher()
{
Console.WriteLine("{0} {1} {2} {3} {4}", nomPre, sexe, taille, poids, numero);
}
}
class Texte
{
static void LireFichier(string nomFichier, Personne[] pers)
{
int n = 0;
Console.WriteLine("\nOn lit le fichier texte " + nomFichier);
StreamReader aLire = File.OpenText(nomFichier);
string ligneLue = null;
while ((ligneLue = aLire.ReadLine()) != null)
{
for (int i = 0; i < pers.Length; i++)
{
pers[i] = new Personne(ligneLue);
}
}
aLire.Close();
}
static void Afficher(Personne[] pers, int nbPers, string message)
{
Console.WriteLine("Contenu du tableau {0} :", message);
for (int i = 0; i < nbPers; i++)
{
pers[i].Afficher();
}
}
static void Main(string[] args)
{
const int max = 32;
Personne[] pers = new Personne[max];
int nbPers = pers.Length;
LireFichier("met_h12.txt", pers);
Afficher(pers, nbPers, "apres lecture du fichier");
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
When I print it, I get
0
0
0
0
0
0
0
0
0
0
my class array is empty for some reason