0

I have the code on C# each object must have the Three Notes but in below i found the same note on all the object :

ArrayList listStagiaire = new ArrayList();
int i;
for (i = 0; i < notes.Length; i++)
{
    Console.WriteLine("Veuillez saisir la note" + i+"\n");
    notes[i] = double.Parse(Console.ReadLine());
}
//Console.ReadLine();
Stagiaire stg1 = new Stagiaire(compteur++, "karami", "loubna", "TDI",notes);
Stagiaire stg2 = new Stagiaire(compteur++, "Jamal", "Youssef", "TDI", notes);
Stagiaire stg3 = new Stagiaire(compteur++, "Ilham", "Fayrouz", "TRI", notes);
listStagiaire.Add(stg1);
listStagiaire.Add(stg2);
listStagiaire.Add(stg3);

Thanks in advance

camille
  • 19
  • 2
  • 1
    Using ArrayList for new development [is discouraged](https://learn.microsoft.com/en-us/dotnet/api/system.collections.arraylist?view=netframework-4.8#remarks) – stuartd Apr 29 '19 at 11:13
  • 1
    Because you reuse the same reference over and over. `notes` – Jeroen van Langen Apr 29 '19 at 11:14
  • you need to ask for the user input of notes after each creation of a `Stagiaire` object (and dont forget to make a `new double[3]`!) – Mong Zhu Apr 29 '19 at 11:24
  • but probably it would be better to make `notes` `public` in `Stagiaire` and remove it from the constructor. then create the 3 objects and put them into `listStagiaire`. Then iterate over `listStagiaire` create each time a new `double[3]` , ask the user for the notes and put the notes into each object : `listStagiaire[i].Notes = notes;` – Mong Zhu Apr 29 '19 at 11:30
  • Bonjour, @camille. Je te recommande la lecture de [ask]. Pour reprendre le commentaire de Stuart et le mien d'il y a deux semaines "Arrêtez avec ArrayList, pour ton propre bien!". Ton code est toujours aussi confus. Une simple phrase de description et un block de code est insuffisant, [edit] ton post avec un peux plus de phrases et de détails. Merci d'avance. – xdtTransform Apr 29 '19 at 11:37
  • 1
    I didn't understand your problem. What exactly do you need? You want each Stagiaire to have 3 own notes? – Ronaldo Araújo Alves Apr 29 '19 at 11:58
  • @RonaldoAraújoAlves yes for each Stagiaire have Three note 15,17,12 – camille Apr 29 '19 at 12:00
  • "yes for each Stagiaire have Three note 15,17,12 " looking at your code => it will produce exactly what you want. But a problem might arise if you change 1 note in one object and expect that this note in the other objects will remain the same – Mong Zhu Apr 29 '19 at 12:14
  • Why 3? Do each intern has the _same_ 3 mark, the 3 marks common to a set of intern? Is one intern limited to 3 mark or may he have more? Is this 3 mark only because we have only 3 mark to enter? Mark(note) looks like a property of Intern(stagiaire) class, questions are: "Is it `double[3]` or a `List`" and "Modifying one Itern mark change the mark of other student sharing it?". – xdtTransform Apr 29 '19 at 12:32

1 Answers1

3

In the light of what been said with all the updates we'd need to change your code quite a bit.

First of all let's change the deprecated ArrayList to List<T> then let's create a function that will get notes for each Stagiaire:

private static double[] GetNotes()
{
   var notes = new double[3];

   for (var i = 0; i < notes.Length; i++)
   {
      do
      {
         Console.Clear();
         Console.WriteLine("Veuillez saisir la note" + i + "\n");
      } while (!double.TryParse(Console.ReadLine(), out notes[i]));
   }
   return notes;
}

And use it:

var listStagiaire = new List<Stagiaire>()
{
   new Stagiaire(compteur++, "karami", "loubna", "TDI", GetNotes()),
   new Stagiaire(compteur++, "Jamal", "Youssef", "TDI", GetNotes()),
   new Stagiaire(compteur++, "Ilham", "Fayrouz", "TRI", GetNotes())
};
Fabjan
  • 13,506
  • 4
  • 25
  • 52
  • the table Notes contains Three notes for one object and for each object have three notes note[0] and notes[1] and notes[2] just for first object stg1 – camille Apr 29 '19 at 11:22