0

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.

4
  • That's what I thought, did you have a look at my answer? Does that not work as you want it to? Commented May 16, 2013 at 1:25
  • No still get the same error if I use your method. Commented May 16, 2013 at 9:34
  • You still get a OutOfBounds exception when changing the line to this? textBox1.Text[a].ToString() ? Commented May 16, 2013 at 9:37
  • No, but im trying to add both a and b to the integerList and then multiply them and show the results in my listbox. It might be better using an int array. Commented May 16, 2013 at 11:39

5 Answers 5

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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;

Comments

0

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

Comments

0

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.

Comments

0

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.

1 Comment

The output in listbox is just 1 if I use this method? No Errors tho.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.