【问题标题】:c# convert.ToInt not working?c# convert.ToInt 不工作?
【发布时间】:2013-04-12 22:10:22
【问题描述】:

我已经尝试了一个小时来解决这个问题,并尝试解析但没有奏效。下面的代码在我转换联赛数据 [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();

下面是联盟课程的代码,谢谢,杰克

职业联赛 { 私有字符串 LeagueName; 私人字符串 LeagueSponsor; 私人 int LeaguePrize; 私人 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;
    }
}

}

【问题讨论】:

  • leagueData[2] 和leagueData[3] 包含什么?
  • 联赛数据[2] 包含联赛奖金,联赛数据[3] 包含联赛内的赛程数。两者都在 getset 期间存储为 int,然后放入数组列表中,然后它不会编译
  • 您不能转换不包含任何内容的变量。我认为 Marty 的问题是,变量是否真的有值,然后再尝试转换它们?

标签: c# type-conversion


【解决方案1】:

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

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

【讨论】:

  • 您不需要调用 string.IsNullOrEmpty -- 如果您将 null 或空字符串传递给 TryParse,它只会返回 false,与任何无效字符串相同。此外,不需要初始化 outNum 变量,因为对 TryParse 的调用会为变量分配一个值。
猜你喜欢
  • 2011-06-15
  • 1970-01-01
  • 1970-01-01
  • 2016-06-23
  • 2013-01-20
  • 2014-07-09
  • 2016-12-15
  • 2013-05-29
  • 1970-01-01
相关资源
最近更新 更多