繁体   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