c#convert.ToInt不工作?

时间:2013-04-12 22:10:22

标签: c# type-conversion

我一直试图解决这个问题一小时,并尝试解析但没有工作。下面的代码在我转换leagueData [2]和联盟数据[3]的两行中给出了错误的输入字符串输入字符串格式不正确。我错过了一些简单的东西吗?

  public static void readLeagues(string theFile, ArrayList allLeagues)
    {
        StreamReader inLeagues = null;
        bool anyMoreLeagues = false;
        string[] leagueData = new string[frmLeagues.numLeagueItems];
        string[] fixtureData = new string[frmLeagues.numFixItems];
        Leagues tempLeague;
        Fixtures tempFix;
        int numFixInLeague, leaguePrize;

        if (fileOpenForReadOK(theFile, ref inLeagues))
        {

            anyMoreLeagues = getNext(frmLeagues.numLeagueItems, inLeagues, leagueData);

            while (anyMoreLeagues == true)
            {
               leaguePrize = Convert.ToInt32(leagueData[2]);
               numFixInLeague = Convert.ToInt32(leagueData[3]);


                tempLeague = new Leagues(leagueData[0], leagueData[1],numFixInLeague,
                                        leaguePrize);

                for (int i = 0; i < numFixInLeague; i++)
                {
                    getNext(frmLeagues.numFixItems, inLeagues, fixtureData);
                    tempFix = new Fixtures(fixtureData[0], fixtureData[1], fixtureData[2]
                                            , fixtureData[3], fixtureData[4]);
                    tempLeague.addFixturesToLeague(tempLeague.getLeagueFixtures(),tempFix);

                }

                allLeagues.Add(tempLeague);
                anyMoreLeagues = getNext(frmLeagues.numLeagueItems, inLeagues, leagueData);
            }
        }
        if (inLeagues != null) inLeagues.Close();

以下是League ClassThanks的代码,杰克

班级联赛     {         私人字符串LeagueName;         私人串联盟赞助商;         private int LeaguePrize;         private int LeagueNumFixtures;         ArrayList LeagueFixtures;

    public Leagues(string inLeagueName, string inLeagueSponsor, int inLeaguePrize,
                    int inLeagueNumFixtures)
    {
        LeagueName = inLeagueName;
        LeagueSponsor = inLeagueSponsor;
        LeaguePrize = inLeaguePrize;
        LeagueNumFixtures = inLeagueNumFixtures;
        LeagueFixtures = new ArrayList();

    }
    public ArrayList addFixturesToLeague(ArrayList fixturesSoFar, Fixtures theNewFixture)
    {
        fixturesSoFar.Add(theNewFixture);
        LeagueNumFixtures = fixturesSoFar.Count;
        return fixturesSoFar;

    }
    public void setLeagueName(string inLeagueName)
    {
        LeagueName = inLeagueName;
    }
    public void setLeagueSponsor(string inLeagueSponsor)
    {

        LeagueSponsor = inLeagueSponsor;
    }
    public void setLeaguePrize(int inLeaguePrize)
    {
        LeaguePrize = inLeaguePrize;
    }
    public void setLeagueNumofFixture(int inLeagueNumFixtures)
    {
        LeagueNumFixtures = inLeagueNumFixtures;
    }
    public void setLeagueFixtures(ArrayList inLeagueFix)
    {
        LeagueFixtures = inLeagueFix;
    }
    public string getLeagueName()
    {
        return LeagueName;
    }
    public string getLeagueSponsor()
    {
        return LeagueSponsor;
    }
    public int getLeaguePrize()
    {
        return LeaguePrize;
    }
    public int getLeagueNumFixtures()
    {
        return LeagueNumFixtures;
    }
    public ArrayList getLeagueFixtures()
    {
        return LeagueFixtures;
    }
}

}

1 个答案:

答案 0 :(得分:1)

我会确保leagueData [2]和leagueData [3]不为null,然后对它们执行TryParse。您还应首先检查leagueData不为null或为空。我假设leagueData是一个字符串数组

var prize = leagueData[2];
int outNum;
int leaguePrize = Int.TryParse(prize, out outNum)? outNum : 0;
  • 根据phoog的评论进行小编辑