Autor Beitrag
DrHoas
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 47



BeitragVerfasst: Fr 11.06.10 12:49 
Hallo,

ich hab eine Klasse Abc erstellt. Außerdem gibt es eine Variable anzahl. Jetzt würde ich gern von i=0 bis anzahl
Abc Nummeri = new Abc()
erstellen, sodass ich dann Abc's mit den Namen Nummer0, Nummer1, Nummer2... hab.

Wie kann man das machen?

Dank und Gruß

Philipp
danielf
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1012
Erhaltene Danke: 24

Windows XP
C#, Visual Studio
BeitragVerfasst: Fr 11.06.10 12:59 
Hallo und :welcome:,

wenn du mehrere Objekt erstellen willst, brauchst du einen Kontainer der diese beinhaltet. Sprich eine Datenstruktur á la Array oder List. Wenn du im Vorhinein schon weißt, wieviele Element es sind und sich an der Anzahl später nichts ändert, ist ein Array wohl die richtige Wahl. Der Variablennamen spielt dabei keine Rolle. Wenn ein Objekt ein eindeutigen Namen haben soll, brauchst du wohl eine Factory die solche Objekte erstelle bzw. ihre Eindeutigkeit gewährleistet. Aber ich denke das ist für deinen UseCase nicht notwendig.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
Abc[] listOfAbcInstances = new Abc[10];

for (int i = 0; i < 10; i++)
{
   Abc[i] = new Abc();
}


Gruß
DrHoas Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 47



BeitragVerfasst: Fr 11.06.10 13:15 
das Ganze tut, wenn ich aus
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
Abc[] listOfAbcInstances = new Abc[10];

for (int i = 0; i < 10; i++)
{
   Abc[i] = new Abc();
}


ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
Abc[] listOfAbcInstances = new Abc[10];

for (int i = 0; i < 10; i++)
{
   listOfAbcInstances[i] = new Abc();
}


mache.

Danke nochmal.

Philipp

Moderiert von user profile iconChristian S.: Code- durch C#-Tags ersetzt