Salut
Je voudrais parcourir et initialiser un tableau a l'aide d'un indexer . En fait voici mon code:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 public class Vorlesungsplan
    {
 
 
        public Vorlesungseinheit[,] plan = new Vorlesungseinheit[5, 6];
 
        public void SchowArray()
        {
            Console.WriteLine("{0 ,-13}{1 ,-13}{2 ,-13}{3 ,-13}{4 ,-13}{5 ,-13}","block","Montag","Dienstag","Mittwoch","Donnerstag","Freitag");
            for (int i = 0; i < plan.GetLength(1); i++)
            {
 
                Console.Write("{0 ,-5}", i + 1);
 
                for (int j = 0; j < plan.GetLength(0); j++)
                {
                    if (plan[j, i] != null)
                    {
                        Console.Write("{0 ,-15}",plan[j, i].ToString());
                    }
                    else
                    {
                        Console.Write("{0 ,-15}", " ");
                    }
                }
            }
        }
et bien j'ai aussi implémenté la classe Vorlesungsheiten avec une Structure Vorlesung:


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
 public struct Vorlesung
    {
        private int nummer;
 
        public String Dozent { get; set; }
        public String kurzbezeichnung { get; set; }
 
 
 
 
        private int Nummer
        {
            get
            {
                return nummer;
            }
        }
 
 
        public Vorlesung(int nummer)
            : this()
        {
            this.nummer = nummer;
        }
 
        public Vorlesung(int nummer,String kurzbezeichnung,String Dozent):
            this()
        {
            this.kurzbezeichnung = kurzbezeichnung;
            this.nummer = nummer;
            this.Dozent = Dozent;
            //Console.WriteLine("{0},{1},{2}", nummer, kurzbezeichnung, Dozent);
 
        }
        public override string ToString()
        {
            return String.Format("{0},{1},{2}",nummer,kurzbezeichnung,Dozent);
        }
    }
   public enum Tag {Montag=0,Dienstag=1,Mittwoch=2,Donnerstag=3,Freitag=4};
 
   public class Vorlesungseinheit
    {
        private Vorlesung vorlesung;
        public Vorlesung Gilles
 
        {
            get
            {
                return vorlesung;
            }
        }
        public String Raum { get; set; }
 
        public Vorlesungseinheit(Vorlesung gilles, String raum)
        {
            this.vorlesung = gilles;
            this.Raum = raum;
            //Console.WriteLine("{0} {1}", gilles.kurzbezeichnung, Raum);
        }
        public override string ToString()
        {
            return String.Format("{0} {1}", vorlesung.kurzbezeichnung, Raum);
        }
    }
 
   }
je dois implémenter un indexer tel que ceci puis s'afficher sur la console:


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public static void Init(Vorlesungsplan plan) {
// Montag
Vorlesung lki = new Vorlesung(261009, "Buck, Jasmin", "L&KI");
Vorlesung dsk = new Vorlesung(261024, "Kraus, Tobias", "DSK");
plan[Tag.Montag, 3] = new Vorlesungseinheit(lki, "A210");
plan[Tag.Montag, 4] = new Vorlesungseinheit(dsk, "A110");
plan[Tag.Montag, 5] = new Vorlesungseinheit(dsk, "A110");
// Dienstag
Vorlesung meba = new Vorlesung(261018, "Buß, Roland", "MEBA");
Vorlesung team = new Vorlesung(260272, "Marsden, Nicola", "TEAM");
plan[Tag.Dienstag, 1] = new Vorlesungseinheit(meba, "A412");
plan[Tag.Dienstag, 2] = new Vorlesungseinheit(meba, "A412");
plan[Tag.Dienstag, 3] = new Vorlesungseinheit(meba, "A412 A106");
plan[Tag.Dienstag, 4] = new Vorlesungseinheit(team, "A411");
// Mittwoch
Vorlesung gse2 = new Vorlesung(261006, "Permantier, Gerald", "GSE2");
Vorlesung sv1 = new Vorlesung(261013, "Doneit, Jürgen", "SV1");
plan[Tag.Mittwoch, 1] = new Vorlesungseinheit(gse2, "A407");
plan[Tag.Mittwoch, 2] = new Vorlesungseinheit(sv1, "F232 F235");
plan[Tag.Mittwoch, 3] = new Vorlesungseinheit(sv1, "D009 F232");
plan[Tag.Mittwoch, 4] = new Vorlesungseinheit(team, "A407");
plan[Tag.Mittwoch, 5] = new Vorlesungseinheit(lki, "A407");
// Donnerstag
Vorlesung bs = new Vorlesung(261007, "Heinz, A.", "BS");
Vorlesung kprog = new Vorlesung(261004, "Jaeger, Ulrike", "KProg");
plan[Tag.Donnerstag, 2] = new Vorlesungseinheit(bs, "F235 F232");
plan[Tag.Donnerstag, 3] = new Vorlesungseinheit(kprog, "F235 F232");
plan[Tag.Donnerstag, 4] = new Vorlesungseinheit(kprog, "F235 F232");
// Freitag
Vorlesung wpsp = new Vorlesung(261026, "Marczinkowsky / Scharpf", "WPSP");
plan[Tag.Freitag, 2] = new Vorlesungseinheit(lki, "F138");
plan[Tag.Freitag, 3] = new Vorlesungseinheit(gse2, "A407");
plan[Tag.Freitag, 4] = new Vorlesungseinheit(wpsp, "F235 F232");
plan[Tag.Freitag, 5] = new Vorlesungseinheit(team, "F235");
}

dans la classe Vorlesungsplan j'ai implémté l'indexer suivant:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public Vorlesungseinheit this[Tag t, int block]
        {
            get
            {
                if (block > 0 && block <= 6)
                {
                  return  plan[(int)t, block];
                }
                return null;
            }
            set
            {
                plan[(int)t, block] = value;
            }
        }
    }
le problème avec cette méthode: je suis obligé de creer un objet de la classe Vorlesungsplan et au lieu de :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
plan[Tag.Montag, 3] = new Vorlesungseinheit(lki, "A210");
plan[Tag.Montag, 4] = new Vorlesungseinheit(dsk, "A110");
plan[Tag.Montag, 5] = new Vorlesungseinheit(dsk, "A110");
je suis obligé de modifier ca comme ceci:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
Vorlesungsplan c = new Vorlesungsplan();
c[Tag.Montag, 3] = new Vorlesungseinheit(lki, "A210");
            c[Tag.Montag, 4] = new Vorlesungseinheit(dsk, "A110");
            c[Tag.Montag, 5] = new Vorlesungseinheit(dsk, "A110");
            c.SchowArray();
et bien sure la mèthode main:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
public static void Main()
        {
 
            Vorlesungsplan v = new Vorlesungsplan();
            Init(v);
        }
je voudrais savoir s'il est possible t'écrire un indexer(avec les paramètre Tag et block) qui me permettra t'initialiser et de parcourir mon tableau avec ceci par exemple:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
plan[Tag.Montag, 3] = new Vorlesungseinheit(lki, "A210");
plan[Tag.Montag, 4] = new Vorlesungseinheit(dsk, "A110");
plan[Tag.Montag, 5] = new Vorlesungseinheit(dsk, "A110");
merci