C# loop string array error
Ok I am confused by this, I have: a int variable a string array and a statement that all should would. Its pretty standard looping array structure.
public class Form1 : System.Windows.Forms.Form
{
public int ticker = 0;
public string[] pictureArray = new String[] {
"image01.jpg",
"image02.jpg",
"image03.jpg",
"image04.jpg",
"image05.jpg",
"image06.jpg",
"image07.jpg",
"image09.jpg",
"image10.jpg",
"image1开发者_Python百科1.jpg",
"image12.jpg",
"image13.jpg",
"image14.jpg",
"image15.jpg",
"image16.jpg",
"image17.jpg",
"image18.jpg",
"image19.jpg",
"image20.jpg",
"image21.jpg",
"image22.jpg"
};
...
if (this.ticker < 21)
{
this.ticker++;
}
else
{
this.ticker = 0;
}
MessageBox.Show(pictureArray[ticker]);
It runs fine until ticker is > 21 then it crashes and states IndexOutOfRange but if I were to say change the MessageBox to just print ticker it is fine and I have no error. Now I have looked through similar questions but the problem is that I am a PHP programmer and I am not sure on some of them if the answers pertain to my situation.
Any help will be greatly appreciated, I think that I have all of the relevant information here if not I apologize. But everything works until it starts to recycle the array and run through the array again. I am just baffled.
You're missing image08.jpg
, which means there are only 21 entries in your array, not 22. Therefore when ticker
is 21, you'll get an exception.
One way of making this more robust is to use:
if (ticker < pictureArray.Length - 1)
{
ticker++;
}
else
{
ticker = 0;
}
Or with a conditional:
ticker = ticker < pictureArray.Length - 1 ? ticker : 0;
Or with Richard's "unconditional increment, conditional reset".
Or possibly the somewhat simpler:
ticker = (ticker + 1) % pictureArray.Length;
Admittedly in this case that would cause you to miss what I suspect is the real problem - you haven't got all the images you're expecting to have.
If ticker = 20, you click it up to 21.
Then you try to index a 0 based 21 element array at position 21.
Your if should be ticker < 20
Edit -
Ha, looks like while this would fix your index problem, Jon's answer is the correct one.
That's pretty funny.
Change it to do this:
MessageBox.Show(pictureArray[ticker]);
ticker++;
if (ticker >= pictureArray.Length)
{
ticker = 0;
}
This way it will also adapt if you change the size of pictureArray.
The problem is that you never read index 0 at all. In the first instance, you increment this.ticker
before reading the first element. You should increment after displaying the value of the string at each array position.
So ultimately, what ends up happening is you set your index to 21
and not 20
thus IndexOutOfRangeException
.
You actually have 21 items you are missing image08.jpg!! so you should change the condition to < 20 . Or in general < PictureArray.Length - 1
BrandonS, the problem with your code is very minute but results badly. Flow of your code :
- First you are checking the value of ticker
- If it is less, you increment to 1, else make it 0
- You print the value of array
So, public string[] pictureArray = new String[] {"image01.jpg","image02.jpg"}; // Array of 2
ticker = 0
if (ticker < 2)
ticker++; // STEP 1
else
ticker = 0;
MessageBox.Show(pictureArray[ticker]); // STEP 2
ticker Image Shown
0 image02.jpg
1 image03.jpg ?????
After checking, you are incrementing and then diaplying, so the displayed value is already 1 ahead of the ticker. To work out their are different ways :- you can replace the if clause (ticker < 21) to (ticker < pictureArray.Length -1). After incrementing to access the previous picture use pictureArray[ticker-1]. OR increment after displaying. Have written a small code that works. The results are also shown.
string[] pictureArray = new String[] {"image01.jpg","image02.jpg};
int ticker = 0;
do
{
if (ticker < pictureArray.Length - 1)
ticker++;
else
ticker = 0;
Console.WriteLine("Ticker = " + ticker + " Image = " + pictureArray[ticker]);
} while (true);
Results :
Ticker = 1 Image = image02.jpg
Ticker = 0 Image = image01.jpg
Ticker = 1 Image = image02.jpg
Ticker = 0 Image = image01.jpg
The below dispaly statements wil lalso result same as above : if (ticker == 0) Console.WriteLine("Ticker = " + ticker + " Image = " + pictureArray[0]); else Console.WriteLine("Ticker = " + ticker + " Image = " + pictureArray[ticker]);
If you can first dispaly and then increment or before incrementing save its value in other var and use that var to display that would result best instead of above ones.
int displayTicker = 0;
if (ticker < 2) {
displayTicker =ticker;
ticker++;
} else {
displayTicker = ticker = 0;
}
MessageBox.Show (pictureArray[displayTicker]);
I believe this option will be the best. In such situation its better to have 2 vars instead of keeping one and having a check over it.
hope this helps.
精彩评论