Jump to content
  • Advertisement
Sign in to follow this  
Enerjak

I'm Getting An Index Out Of Bounds When Trying To Add Buttons Pragmatically.

This topic is 732 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I thought I'd try my hand at adding buttons pragmatically using arrays. I have this little function that I thought would work well enough: 

private bool createButtons()
        {
            bool bButtonsCreated = false;
            int row = NumPad.RowCount;
            int col = NumPad.ColumnCount;
            int i = 1;
            int j = 1;
            int num = (i * j);
            for (i = 1; i < row; i++)
            {
                for(j = 1; i < col; j++)
                {
                    // set the text for the buttons.
                    CalcButtons = new Button[num];
                    CalcButtons[num] = new Button();
                    CalcButtons[num].Text = "7";
                    CalcButtons[num - 1].Text = "8";
                    CalcButtons[num - 2].Text = "9";
                    CalcButtons[num - 3].Text = "4";
                    CalcButtons[num - 4].Text = "5";
                    CalcButtons[num - 5].Text = "6";
                    CalcButtons[num - 6].Text = "1";
                    CalcButtons[num - 7].Text = "2";
                    CalcButtons[num - 8].Text = "3";
                    CalcButtons[num].Size = new Size(60, 60);
                }
            }
            if(CalcButtons[num] != null)
            {
                bButtonsCreated = true;
                NumPad.Container.Add(CalcButtons[num]);
            }
            else
            {
                MessageBox.Show("Buttons not created", "Error", MessageBoxButtons.OK);
            }
            return bButtonsCreated;
        }

However, as you can see, it gives out an indexOutOfBounds exception: 

 

3881d80835.png

 

What can I do to fix this? Is this the right way to use arrays to do this? 

Share this post


Link to post
Share on other sites
Advertisement

Looking at the inner loop, you have

 

for(j = 1; i < col; j++)

 

I think you want "j < col" instead.

 

Also, you don't seem to recompute "num" inside the loop... it'll always have the same value. But you are recreating your array every time inside the loop which is incorrect....

Edited by gfxgangsta

Share this post


Link to post
Share on other sites

Looking at the inner loop, you have

 

for(j = 1; i < col; j++)

 

I think you want "j < col" instead.

 

Should I do that for the outer loop as well? 

 

Edit: Ignore this, I got what you meant. 

Edited by Enerjak

Share this post


Link to post
Share on other sites

The reason you're getting out of bounds is because "num" is 1. And you're creating an array of size 1. But then you're trying to access the array at index 1 (and arrays are zero-based).

Share this post


Link to post
Share on other sites

There are other issues as well, I think...

Your for loops use i and j in their conditions, but they aren't used in the for loops.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!