简体   繁体   中英

Index out of bounds exception while iterating through a List<string>

I have a List of the location of images on a folder.

I have five pictureBoxes that emulate a coverflow type area for users to skim through the images of a given folder.

I know that the error is fired because the first image in the collection is set to the first picturebox, then if I click on cycleLeft(), there is a negative number.

How can I account for this? For example, if the first image in the List is already set to the farthest left and someone clicks flip left, have that first image put on the last position of the list.

Any guidance?

    private void leftArrow_Click(object sender, EventArgs e)
    {
        cycleImagesLeft();
    }

    private void rightArrow_Click(object sender, EventArgs e)
    {
        cycleImagesRight();
    }

    public void cycleImagesLeft()
    {
        //imageThree is the center image, that's why I use it as a frame of reference.
        int currentImage = pictures.IndexOf(imageThree.ImageLocation);
        imageOne.ImageLocation = pictures[currentImage - 3];
        imageTwo.ImageLocation = pictures[currentImage - 2];
        imageThree.ImageLocation = pictures[currentImage - 1];
        imageFour.ImageLocation = pictures[currentImage];
        imageFive.ImageLocation = pictures[currentImage + 1];
    }

    public void cycleImagesRight()
    {
        int currentImage = pictures.IndexOf(imageThree.ImageLocation);
        imageOne.ImageLocation = pictures[currentImage - 1];
        imageTwo.ImageLocation = pictures[currentImage];
        imageThree.ImageLocation = pictures[currentImage + 1];
        imageFour.ImageLocation = pictures[currentImage + 2];
        imageFive.ImageLocation = pictures[currentImage + 3];
    }

Well, one option would be to use a helper method to ensure that the value is always within bounds:

string GetPictureAt(int index)
{
    // Copes with values which are two large or too small,
    // but only as far as -pictures.Length
    if (index < 0)
    {
        index += pictures.Length;
    }
    return pictures[index % pictures.Length];
}

Then:

public void CycleImagesLeft()
{
    int currentImage = pictures.IndexOf(imageThree.ImageLocation);
    imageOne.ImageLocation = GetPictureAt(currentImage - 3);
    imageTwo.ImageLocation = GetPictureAt(currentImage - 2);
    // etc
}

and the same for CycleImagesRight() . I think this does what you want, but I didn't quite follow your penultimate sentence.

Note that you still need to account for the possibility of there being fewer than 5 pictures.

Could always use a Circular List . At least that way all the index management stuff is abstracted away (into a testable and re-usable class).

imageOne.ImageLocation = (currentImage - 3 < 0) ? null : pictures[currentImage - 3];
....
imageFour.ImageLocation = (currentImage + 2 >= pictures.Count) ? null : pictures[currentImage + 2];

etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM