Index out of bounds C # (I'm trying to add savegame and use WriteAllLines)

I'm trying to create savegame for a fairly simple game, and this is the code I'm using now to write money to a .txt file (NameBox is the text box that 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 error outside the bounds of any row after

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

I also tried to do this:

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

But this gives me an error on the closing body (squiggly bracket, as I call it) I did savegame before when the array of walls and soldiers save their sizes and locations using this code (wallList [i] .ToString () refers to the method in the class walls, which returns all 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();
    }

, - ! (, , , , , .ToString() int )

+4
5
String[] Money = new String[MainForm.Money];

MainForm.Money. , MainForm.Money 10. - 10, 10 . 0-9.

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

Money [10], 9. , " " .

+6

:

0, :

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

:

, , :

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

, MoneyForm.Money int, , 5000: Money 5000 .

, 5000 ( "5000") 5000- .

Money[5000] = "5000";

5000 "", 0-4999, 0. 5000 " " .

+2

, Mainform.Money, 0 Mainform.Money - 1, Money [Mainform.Money], , .

0

Since you are filling the array of strings Moneywith a length equal to the value MainForm.Money, the maximum possible index for the array is MainForm.Money -1not 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
0
source

In the line below, you initialized Money to be an x ​​array of sum

    strings (x = MainForm.Money).

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

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

Source: https://habr.com/ru/post/1684701/


All Articles