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 )