Autor Beitrag
BCT1
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 36



BeitragVerfasst: Do 25.02.10 17:28 
Hallo, ich wollte mal fragen wie man ein Array erstellt indem jeder Eintrag ein Objekt einer bestimmten Klasse ist. Ich wollte nämlich Namen, Telefonnummern und änliches in ein Array speichern und jeder Arrayeintrag ist zB von der Klasse: "Personen". Hab selbst schon probier aber mit der normal arraydeklaration komm ich irgentwie nicht weiter. Ich hoffe ihr könnt mir helfen.

Gruß BCT1
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: Do 25.02.10 18:30 
Hallo und :welcome:

Eine Klasse steht für ein einzelnes Objekt. Also sollte die Klasse im Singular und die Liste im Plural bezeichnet werden. Damit geht es (vereinfacht gesagt) so:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
class Person { ... }

Person[] personen = new Person[17];
Person ich = new Person();
ich.Name = "ich";
personen[0] = ich;

Beachte: Mit dem Erzeugen des Arrays ist nur festgelegt, dass es ein Person-Array gibt; es werden damit noch keine Instanzen der Klasse Person erstellt. Ein Array hat eine feste Größe; flexibler ist eine List<T>, hier also List<Person>.

Das nur auf die Schnelle (ich hoffe, ich habe mich dabei nicht vertan); ich hoffe, ich konnte trotzdem etwas helfen.

Gruß Jürgen
BCT1 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 36



BeitragVerfasst: Do 25.02.10 18:46 
Danke für die schnelle Antwort, funktioniert super. Vielen Dank