4

I am trying to make savegame for a quite simple game, and this is the code I am using right now to write money to a .txt file (NameBox is the textbox you use to write the name of the .txt file):

private void SaveBtn_Click(object sender, EventArgs e)
    {
        String filename = NameBox.Text;
        if (filename == "")
        {
            filename = "New Save";
        }
        filename += ".txt";
        String[] Money = new String[MainForm.Money];
        Money[MainForm.Money] = MainForm.Money.ToString();
        System.IO.File.WriteAllLines(filename, Money);
        Application.Exit();
    }

However I get an index out of bounds error on whatever line is after

Money[MainForm.Money] = MainForm.Money.ToString();

I've also tried doing this:

for (int i = 0; i < MainForm.Money; i++){
    Money[MainForm.Money] = MainForm.Money.ToString();
}

But it gives me an error on the closing body (squiggly bracket as I call it) I have done a savegame before with an array of walls and soldiers saving their sizes and locations using this code (wallList[i].ToString() refers to a method in the wall class that returns all the values):

private void SaveBtn_Click(object sender, EventArgs e)
    {
        String filename = filenametxt.Text;
        if (filename == "")
        {
            filename = "Level";
        }
        filename += ".txt";
        String[] lines = new String[MainForm.wallList.Count + 
        MainForm.soldierList.Count+1];
        for (int i = 0; i < MainForm.wallList.Count; i++)
        {
            lines[i] = MainForm.wallList[i].ToString();
        }
        lines[MainForm.wallList.Count] = "@";
        for (int i = 0; i < MainForm.soldierList.Count; i++)
        {
            lines[i + MainForm.wallList.Count +1] = 
            MainForm.soldierList[i].ToString();
        }
        System.IO.File.WriteAllLines(filename, lines);
        Application.Exit();
    }

I would very much appreciate if someone could help me! (Please explain to me what each part of the code means, for example explaining that .ToString() converts an int into a string)

jomoetnt
  • 51
  • 4
  • An IndexOutOfBoundsException means that you are attempting to access a part of a collection that doesn't exist. The index is either too high or too low. Imagine your collection as a line of people, you're trying to get person X but there is no person X. A better understanding of this exception will probably make it more clear to you why this is occurring. – Maritim Aug 29 '17 at 20:33
  • Yes, I know what an index out of bounds exception is, I'm just not sure why it is happening. It happened when I did the savegame for the over game I mentioned, but I managed to fix it by playing around with the code like I did here. Thank you for your help! – jomoetnt Aug 29 '17 at 20:36

5 Answers5

6
String[] Money = new String[MainForm.Money];

Here you create the array of size MainForm.Money. Say the value of MainForm.Money is 10. The array is size 10, meaning there are 10 slots. These are numbered 0-9.

Money[MainForm.Money] = MainForm.Money.ToString();

Here you do the equivalent of Money[10] when it only has slots up to 9. Thus you go "Out of Bounds" of the array.

Tyler
  • 955
  • 9
  • 20
2

In short:

Arrays are 0-based so this array access is the problem:

Money[MainForm.Money] = ... // MainForm.Money-1 would be okay

In more detail:

So we have these two lines causing your problem:

String[] Money = new String[MainForm.Money];
Money[MainForm.Money] = MainForm.Money.ToString();

Assuming that MoneyForm.Money is an int - let's pretend it is 5000: In this case you are creating a string array Money with 5000 empty slots to store strings in.

In the second line you basically say that you want to write that 5000 as string (so "5000") in the array's 5000th element.

Money[5000] = "5000";

But when you declare an array with 5000 "slots" you have them from 0-4999 because arrays are 0-based. That's why 5000 is "out of bounds".

Waescher
  • 5,361
  • 3
  • 34
  • 51
0

It appears that you are initializing an array with the length of Mainform.Money that starts at 0 up to Mainform.Money - 1, but when you try to set the value of Money[Mainform.Money], you are setting that value outside the defined array.

  • Thank you all for the very quick responses! Although I now know what the problem is, can anyone tell me how I can fix this? Thanks! – jomoetnt Aug 29 '17 at 20:42
0

Since you are populating the string array Money with length equal to the value of MainForm.Money the maximum possible index for the array is equal to MainForm.Money -1 not MainForm.Money:

    Money[MainForm.Money] = MainForm.Money.ToString(); 
// you should change the index here to be within the range (0 - MainForm.Money -1) based on your requirements
Abdullah Dibas
  • 1,499
  • 1
  • 9
  • 13
0

In the line below, you initialized the Money to have be an array of x amount of

    strings (x = MainForm.Money).

Then you tried to access item x + 1 from the line below, which caused the exception. If you want to access the last element of the array, you should call Money[MainForm.Money - 1].

    Money[MainForm.Money] = MainForm.Money.ToString();
Kenneth Song
  • 51
  • 1
  • 5