ERLEDIGT
NEIN
NEIN
ANTWORTEN
12
12
ZUGRIFFE
1611
1611
EMPFEHLEN
-
Hallo, ich habe folgendes problem:
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
for (int i = 0; i < int.Parse(txtanzahlad.Text); i++) { for (int j = 1; j <= 5; j++) { TextBox txtping = new TextBox(); txtping.Name = "txtping" + i.ToString() + "_" + j.ToString(); txtping.Location = new Point(iX+300+pingx, iY); txtping.Size = new Size(67, 20); txtping.Visible = true; txtping.ReadOnly = true; panel1.Controls.Add(txtping); pingx += 80; } }
dies möchte ich zweidimensional machen. ein einfaches Array funktioniert....aber ich kann es nicht zwei dimensional machen...
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
//in public partial class Form1 : Form TextBox[][] txtping; //vor meiner schleife txtping = new TextBox[int.Parse(txtanzahlad.Text)][5]; //meine angepasste schleife: for (int i = 0; i < int.Parse(txtanzahlad.Text); i++) { for (int j = 1; j <= 5; j++) { txtping[i][j] = new TextBox(); txtping[i][j].Name = "txtping" + i.ToString() + "_" + j.ToString(); txtping[i][j].Location = new Point(iX+300+pingx, iY); txtping[i][j].Size = new Size(67, 20); txtping[i][j].Visible = true; txtping[i][j].ReadOnly = true; panel1.Controls.Add(txtping[i][j]); pingx += 80; }
wenn ich es so mache, gibt er mir ein fehler aus.
ich hoffe ihr könnt mir weiterhelfen
-
"The three chief virtues of a programmer are: Laziness, Impatience and Hubris."
--- Larry Wall
-
Problem ist
<=5
muss
< 5
lauten
Der Index beginnt ja bei 0, und die Länge beträg 5,
0
1
2
3
4
= 5 Einträge (soviel wie du im Array alloziiert hast)
(Ändert auch nixs dran wenn du in der Schleife bei 1 beginnst
)
-
Ein Fehler ist auf jeden Fall, dass j von 1 bis 5 läuft, die entsprechende Dimension des Arrays aber auf 5 dimensioniert ist, also kein Element mit dem Index 5 hat (4 ist der höchste Index). Das gibt eine IndexOutOfRangeException beim letzten Durchlauf der inneren Schleife.Du musst entweder j von 0 bis 4 laufen lassen oder die zweite Dimension von txtping auf 6 erhöhen. Ersteres ist sauberer, da man bei Array-Indizes generell bei 0 beginnt zu zählen.
Gruß
Stefan
-
danke für eure antwort,
wenn ich es so habe:
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
txtping = new TextBox[int.Parse(txtanzahlad.Text)][]; for (int j = 0; j < 5; j++) { txtping[i][j] = new TextBox(); txtping[i][j].Name = "txtping" + i.ToString() + "_" + j.ToString(); txtping[i][j].Location = new Point(iX+300+pingx, iY); txtping[i][j].Size = new Size(67, 20); txtping[i][j].Visible = true; txtping[i][j].ReadOnly = true; panel1.Controls.Add(txtping[i][j]); pingx += 80; }
bekomme ich folgenden fehler
Code :1
Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
wenn ich es aber so mache :
sagt er mir sofortCode :1
txtping = new TextBox[int.Parse(txtanzahlad.Text)][5];
Code :1
Fehler 1 Ungültiger Rangbezeichner: Erwartet wird "," oder "]".
mhmm
-
26.02.10 08:05 #6
- Registriert seit
- Jun 2005
- Beiträge
- 8.168
Hi.
Warum suchst du denn nicht erstmal?
"c# mehrdimensionale arrays"
GrußIf at first you don't succeed, try again. Then quit. No use being a damn fool about it.
-
Das sind Grundlagen - du musst das so schreiben:
Code csharp:1
txtping = new TextBox[int.Parse(txtanzahlad.Text), 5];
Gruß
MCoder"The three chief virtues of a programmer are: Laziness, Impatience and Hubris."
--- Larry Wall
-
Ja das habe ich auch schon gemacht, habe schon einen kompletten Arbeitstag investiert, finde aber den Fehler nicht. Bei "normalen" zweidimensionalen array funktioniert es ja, aber irgendwie sagt er mir hier
Code :1
txtping[i][j] = new TextBox();
einen fehler. und ich weiß nicht warum
-
26.02.10 08:11 #9
- Registriert seit
- Jun 2005
- Beiträge
- 8.168
If at first you don't succeed, try again. Then quit. No use being a damn fool about it.
-
Danke für deine Antwort MCoder, aber wenn ich es so schreibe, kommt folgender Fehler:
Code :1
Fehler 1 Eine implizite Konvertierung vom Typ "System.Windows.Forms.TextBox[*,*]" in "System.Windows.Forms.TextBox[][]" ist nicht möglich.
mhmm aber es liegt nicht schon hier drann oder?
Code :1
TextBox[][] txtping;
-
Ja es kommt immer der Fehler:
Code :1
Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
-
26.02.10 08:18 #12
- Registriert seit
- Jun 2005
- Beiträge
- 8.168
Doch. Schau dir doch mal an wie man mehrdimensionale Array deklariert => http://msdn.microsoft.com/de-de/libr...8VS.80%29.aspx
Weil du das Array nicht ordentlich initialisiert hast.
Gruß
GrußIf at first you don't succeed, try again. Then quit. No use being a damn fool about it.
-
ich hab es nun so gemacht:
Code :1
TextBox[,] txtping;
Code :1
txtping = new TextBox[int.Parse(txtanzahlad.Text), 5];
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14
for (int j = 0; j < 5; j++) { txtping[i,j] = new TextBox(); txtping[i,j].Name = "txtping" + i.ToString() + "_" + j.ToString(); txtping[i,j].Location = new Point(iX+300+pingx, iY); txtping[i,j].Size = new Size(67, 20); txtping[i,j].Visible = true; txtping[i,j].ReadOnly = true; panel1.Controls.Add(txtping[i,j]); pingx += 80; }
und nun funktioniert alles
Vielen Dank euch allen
Ähnliche Themen
-
[perl] Vorhandes Array [Name;Vorname/n] in zweidimensionales Array splitten
Von FlockY im Forum CGI, Perl, Python, Ruby, Power ShellAntworten: 3Letzter Beitrag: 31.08.09, 18:53 -
Zweidimensionales Array von VB
Von si031006 im Forum PHPAntworten: 10Letzter Beitrag: 22.12.06, 11:19 -
Problem mit zweidimensionales Array
Von angelikamorgan im Forum PHPAntworten: 3Letzter Beitrag: 18.04.06, 14:25 -
Zweidimensionales Array
Von crazyPower im Forum PHPAntworten: 2Letzter Beitrag: 04.07.05, 16:03 -
[c++] zweidimensionales dynamisches array
Von andreas_gierisch im Forum C/C++Antworten: 34Letzter Beitrag: 22.10.04, 14:22





Zitieren
Login





