c#转换.它不工作

本文关键字:工作 转换 | 更新日期: 2023-09-27 18:08:50

我一直在试图找出这个一个小时了,现在已经尝试解析,但没有工作。下面的代码在转换联赛数据[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, Jack的代码

类联盟{私有字符串联赛名称;私有字符串;private int联赛奖;private int league enumfixtures;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;
    }
}

}

c#转换.它不工作

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

var prize = leagueData[2];
int outNum;
int leaguePrize = Int.TryParse(prize, out outNum)? outNum : 0;
  • 对每张照片的评论进行小编辑