C
C#14mo 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
Tvde114mo ago
Are you familiar with what a foreach does? For example can you tell me what i means in this loop
Sanne
Sanne14mo 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
Tvde114mo 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
Accord14mo 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
More Posts
❔ Can't add css in ASP.NETHello! I want to add css file in my ASP.NET project, but it's not working. My browser don't see my c❔ Custom cert validation in http clientI have a HttpClient with a SocketsHttpHandler that I'm trying to add a custom certificate to, becaus❔ Android Debugging getting slower and slowerGreetings, I am working on an app for my job. Everything used to be fine but for the past month, deb❔ Converting from binary to integerRecently I had to problem where I would convert an integer to binary and count the ones, the problem❔ Better way to open a "window" to take in text input?As in, when you click a button, a small window appears which exists only to take in the for example ❔ Can someone help me use data from database tables?I have 2 $ and a dream ! Tommorow im going to the national stage of the c# olympiad and i dont even ❔ List string variablesHello, I'm really new to C# and would like to get help. How is the correct way to give variable for❔ EF successfully updated database, but unable to connect to sql server when getting data.Hey guys, getting back into .net after a while. sql server is currently on docker and I am able to c❔ ✅ Writing to response body is slow in .net core 7 web api?Hey guys, I have a stack overflow thread about the issue, but basically, I'm getting significant per❔ update-package : Failed to add reference to 'xyz'. Error HRESULT E_FAIL has been returnedSo I don't really know what happened. Turned my pc on, opened a project I created 2 days ago. My pac