G Gr
G Gr

Reputation: 6090

Index outside array inside loop

I get an error Index was outside the bounds of the array, im trying to do some simple math and holding it in my list.

        List<int> integerList = new List<int>();
        for (int a = 0; a < textBox1.Text.Length; a++)
        {
            for (int b = 8; b > 1 ; b--)
            {
                integerList.Add(int.Parse(textBox1.Text[a * b].ToString())); //this line
            }


        }
        listBox1.DataSource = integerList;

What I am trying to achieve is this, a user must enter a 7 digit number into the textbox say for instance 4565457, I wanted to store this number in my integerList, then take each number starting from the beginning of the users input and multiply down from 8 untill 2 is reached.

For instance:

4 x 8 
5 x 7
6 x 6
5 x 5
4 x 4
5 x 3
7 x 2

I wanted to then store the sum of these multiplications for later use.

Upvotes: 0

Views: 132

Answers (5)

TheVillageIdiot
TheVillageIdiot

Reputation: 40527

Sorry didn't got time to read the edited question. According to new description, if user enters 4565457 you want to multiply each character in that string from position 0 to last (6 in this case) with 8 - position. Following loop single for loop will do the trick.

List<int> integerList = new List<int>();
var l = textBox1.Text.Length;
for (int a = 0, b = 8; a < l && b > 1; a++, b--)
{
    integerList.Add((Convert.ToInt16(textBox1.Text[a]) * b)); //this line
}
listBox1.DataSource = integerList;

At the end of loop, listBox1 will contain following values:

416
371
324
265
208
159
110

Try this:

List<int> integerList = new List<int>();
var l = textBox1.Text.Length;
for (int a = 0; a < l; a++)
{
    for (int b = 8; b > 1 && (a*b) < l ; b--)
    {
        integerList.Add(int.Parse(textBox1.Text[a * b].ToString())); //this line
    }
}
listBox1.DataSource = integerList;

a * b is getting past the length of text in textbox so adding && (a*b) > l will keep this in control.

Upvotes: 0

evanmcdonnal
evanmcdonnal

Reputation: 48124

This line textBox1.Text[a * b] is likely the source of the problem. There's no bounds checking and a * b likely evaluates to an integer greater than the final index of textBox1. I don't have an great solution for you because I don't really understand what you're trying to do... The expression inside the inner for loop just generally does not make much sense.

You could simply check that a * b is less than textBox1.Text.Length in order to prevent the exception but that probably won't make the code do what you actually want it to.

Upvotes: 0

Chief Wiggum
Chief Wiggum

Reputation: 2934

Try this (since you probably want to calc the value of your a number by the value of b):

    List<int> integerList = new List<int>();
    for (int a = 0; a < textBox1.Text.Length; a++)
    {
        for (int b = 8; b > 1 ; b--)
        {
            integerList.Add(int.Parse(textBox1.Text[a].ToString()) * b); //this line
        }


    }
    listBox1.DataSource = integerList;

Upvotes: 1

TGH
TGH

Reputation: 39278

The index a*b is outside the bounds of the TextBox content. You have to put in place safeguards to make sure you don't index the string content of the textbox using an index that is out of bounds

Upvotes: 0

McGarnagle
McGarnagle

Reputation: 102793

It's probably the textBox1.Text that's out of bounds. Try adding a check before using the indexer:

if (a*b < textBox1.Length)
    integerList.Add(int.Parse(textBox1.Text[a * b].ToString())); //this line

Upvotes: 1

Related Questions