简体   繁体   中英

xna 4.0 error converting from string[] to int[]

ok om trying to load from a .txt file into an array of ints to a 2d array but its thowing me "An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll" i believe its something to do with the loops and perhaps going out of bounds. But im new to c# so im stumped.

it breaks on "nRow[r] =Convert.ToInt32(row[r]);"

protected void read_lvl()
    {
        IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
        IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("myFile.txt", FileMode.Open, FileAccess.Read);
        using (StreamReader reader = new StreamReader(fileStream))
        {    //Visualize the text data in a TextBlock text

            while (!reader.EndOfStream)
            {
                //for each row
                for (int i = 0; i < rows; i++)
                {
                    //read in the line
                    string myLine = reader.ReadLine();
                    //take out the commas
                    string[] row = myLine.Split(',');

                    //convert to string to ints
                    int[] nRow = new int[row.Length];
                    for(int r=0; r<row.Length;r++){
                        nRow[r] =Convert.ToInt32(row[r]);
                    }

                    //feed back into the array
                    for (int j = 0; j < columns; j++){
                        myreadArray[i, j] = nRow[j];
                    }
                }
            }

        }
    }

You should use Int32.TryParse if row[r] may not be an integer.

http://msdn.microsoft.com/en-US/library/vstudio/f02979c7.aspx

Use like

int nb = 0;

if (Int32.TryParse("12", out nb) == false)
{
      Console.WriteLine("Error");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM