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",
"image11.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 whentickeris 21, you’ll get an exception.One way of making this more robust is to use:
Or with a conditional:
Or with Richard’s “unconditional increment, conditional reset”.
Or possibly the somewhat simpler:
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.