(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:
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);
}
}
}