Resolved Index was outside the bounds of the array?

Status
Not open for further replies.

LegitMeme

Member
Joined
May 5, 2019
Messages
13
Programming Experience
Beginner
(Reposting this: My last thread was randomly removed?)

I'm currently making a web application, and I'm trying to make an array of Images, but it isn't working? It says the array is out of bounds even tho the numbers are definitely correct?
Are there any ways I can fix this? Thanks!

Code:
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Casino
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                firstImage.ImageUrl = "Bar.png";
                middleImage.ImageUrl = "Bar.png";
                lastImage.ImageUrl = "Bar.png";
            }
        }

        protected void spinButton_Click(object sender, EventArgs e)
        {
            double Money, currentBet;
            int first, second, third;

            currentBet = (double.Parse(betText.Text));

            if (double.Parse(balanceLabel.Text) >= currentBet)
            {
                Money = (double.Parse(balanceLabel.Text));
            }
            else
            {
                return;
            }

            getRandoms(out first, out second, out third);
            Image[] images = getImages();

            checkOutcome(first, second, third, images, Money, currentBet);
        }

        private void checkOutcome(int first, int second, int third, Image[] Images, double Money, double currentBet)
        {
            firstImage = Images[first];
            middleImage = Images[second];
            lastImage = Images[third];

            double playerMoney = Money;
            double multiplier = 1;

            if (first == 2)
            {
                multiplier += 1;
            }
            if (second == 2)
            {
                multiplier += 1;
            }
            if (third == 2)
            {
                multiplier += 1;
            }

            if (first == 0 || second == 0 || third == 0)
            {
                multiplier = 1;
            }

            if (first == 9 && second == 9 && third == 9)
            {
                multiplier = 100;
            }

            currentBet *= multiplier;

            if (multiplier > 1)
            {
                playerMoney += (currentBet * multiplier) - currentBet;
                resultLabel.Text = string.Format("Your money has raised from {0} to {1}!", Money, playerMoney);
            }
            else
            {
                playerMoney -= currentBet;
                resultLabel.Text = string.Format("Your money has lowered from {0} to {1}!", Money, playerMoney);
            }

            balanceLabel.Text = playerMoney.ToString();
        }

        private Image[] getImages()
        {
            Image[] images = new Image[11];

            images[0] = new Image();
            images[1] = new Image();
            images[2] = new Image();
            images[3] = new Image();
            images[4] = new Image();
            images[5] = new Image();
            images[6] = new Image();
            images[7] = new Image();
            images[8] = new Image();
            images[9] = new Image();
            images[10] = new Image();
            images[11] = new Image();

            images[0].ImageUrl = "Bar.png"; //System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
            images[1].ImageUrl = "Bell.png";
            images[2].ImageUrl = "Cherry.png";
            images[3].ImageUrl = "Clover.png";
            images[4].ImageUrl = "Diamond.png";
            images[5].ImageUrl = "HorseShoe.png";
            images[6].ImageUrl = "Lemon.png";
            images[7].ImageUrl = "Orange.png";
            images[8].ImageUrl = "Plum.png";
            images[9].ImageUrl = "Seven.png";
            images[10].ImageUrl = "Strawberry.png";
            images[11].ImageUrl = "Watermellon.png";

            return images;
        }

        private void getRandoms(out int first, out int second, out int third)
        {
            Random image1 = new Random();
            Random image2 = new Random();
            Random image3 = new Random();

            first = image1.Next(0, 11);
            second = image1.Next(0, 11);
            third = image1.Next(0, 11);
        }
    }
}
 
This Image[] images = new Image[11]; creates an array with valid element indexes ranging from 0 to 10. Your code here:
C#:
            images[0] = new Image();
            images[1] = new Image();
            images[2] = new Image();
            images[3] = new Image();
            images[4] = new Image();
            images[5] = new Image();
            images[6] = new Image();
            images[7] = new Image();
            images[8] = new Image();
            images[9] = new Image();
            images[10] = new Image();
            images[11] = new Image();
goes up to 11.

Obligatory Spinal Tap link...
 
OHHH, thank you so much! It is fixed completely, I didn't know that it only went to 10. The only thing I'm confused about is that creepily fitting video you got there.
 
I think its worth noting that you don't need multiple declared instances of random, when you can just use one instance. As each call to new Random(); will generate a new random, thus making this kinda irrelevant.
C#:
        {
            Random image1 = new Random();
            Random image2 = new Random();
            Random image3 = new Random();

            first = image1.Next(0, 11);
            second = image1.Next(0, 11);
            third = image1.Next(0, 11);
        }
Why do you have multiple instances, yet only use one of them?
 
This exception means that you're trying to access a collection item by index, using an invalid index. An index is invalid when it's lower than the collection's lower bound or greater than or equal to the number of elements it contains. whenever we try to access c# array or collection, etc. we can only access the existing indexes in the array. array[0] and array[1] are existing. If we try to access array, it's not there actually, so an index out of bound exception will arise. Typically, an IndexOutOfRangeException exception is thrown as a result of developer error. Instead of handling the exception, you should diagnose the cause of the error and correct your code.
 
An index is invalid when it's lower than the collection's lower bound or greater than or equal to the number of elements it contains.
That's incorrect. Out of bounds exception would be thrown if the index of the array are more or less than the indexes of the arrays defined bounds. If one index of a number of the elements in the array is equal to a specified value, it is obviously a match.

This topic is also over a year old. I'm sure they've resolved this issue by now.
Closing...
 
Status
Not open for further replies.
Back
Top Bottom