C
C#2y ago
Sanne

❔ ✅ Messagebox doesn't start at 1 & error after

int[] numbers = {1, 2, 3, 4, 5};

private void label1_Click(object sender, EventArgs e)
{
foreach (int i in numbers)
{
MessageBox.Show(numbers[i].ToString());
}
}
int[] numbers = {1, 2, 3, 4, 5};

private void label1_Click(object sender, EventArgs e)
{
foreach (int i in numbers)
{
MessageBox.Show(numbers[i].ToString());
}
}
this is my code, but the messagebox doesn't show 1, it starts at 2. after 5, it gives me the error 'index was outside the bounds of the array'. why?
4 Replies
Tvde1
Tvde12y ago
Are you familiar with what a foreach does? For example can you tell me what i means in this loop
Sanne
SanneOP2y ago
kinda, but i just figured it out lol. i think its supposed to do the code for every thing there in the array is right? but changing numbers[i] to just 'i' works! :D
Tvde1
Tvde12y ago
Yep :) Trying with different numbers would also have shown you something weird would happen You can /close this thread if your question is solved ^^
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server