簡體   English   中英

xna 4.0錯誤從string []轉換為int []

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

好的om試圖從.txt文件加載到一個int數組到一個2d數組中,但是它讓我感到“ mscorlib.dll中發生了'System.FormatException'類型的未處理的異常”,我相信這與循環有關也許越界。 但是我對C#還是陌生的,所以我很困惑。

它在“ 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];
                    }
                }
            }

        }
    }

如果row [r]可能不是整數,則應使用Int32.TryParse。

http://msdn.microsoft.com/zh-CN/library/vstudio/f02979c7.aspx

使用方式

int nb = 0;

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM