【问题标题】:C# Trying to type cast array valuesC#尝试键入强制转换数组值
【发布时间】:2013-01-06 01:39:40
【问题描述】:

当尝试从以下代码中的数组字符串值转换 int 时;

   using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;


    namespace hourscount
    {
        class Program
        {
            static void Main(string[] args)
            {
                string delimiter = ":";
                string time1 = Console.ReadLine();
                string time2 = Console.ReadLine();

                if (time1 == null || time2 == null)
                {
                    Console.WriteLine("Program expects two values!");
                    Console.ReadLine();

                }
                else
                {
                    string[] time1var = time1.Split(new string[] {delimiter}, StringSplitOptions.None);
                    string[] time2var = time2.Split(new string[] { delimiter }, StringSplitOptions.None);
                    int time2Intvar1 = int.TryParse(time2var[0]);
                    int time1Intvar1 = int.TryParse(time1var[0]);
                    int time2Intvar2 = int.TryParse(time2var[1]);
                    int time1Intvar2 = int.TryParse(time1var[1]);
                    int realHours = (time2Intvar1 - time1Intvar1);
                    Console.ReadLine();
                }
            }

        }
    }

我收到以下错误;错误 1 ​​方法“TryParse”没有重载需要 1 个参数

【问题讨论】:

  • 这不是强制转换,而是解析和/或转换。使用 Intellisense,为什么您不明白需要额外的参数?我不太明白。
  • 我认为 timeXvar[X] 是论据。
  • 附加参数。
  • 告诉我你在哪里看到了Additional这个词
  • 这是我对您问题的第一条评论,就像白天一样。这与您在 Intellisense 中错过有关此参数的信息的原因相同吗?

标签: c# tryparse


【解决方案1】:

把它当作

int time2Intvar1;
bool isOK = int.TryParse(time2var[0],out time2Intvar1);

更多信息见

http://www.dotnetperls.com/int-tryparse

http://msdn.microsoft.com/en-us/library/f02979c7.aspx

【讨论】:

    【解决方案2】:

    您需要为int.TryParse 提供out parameter

    int time2Intvar1;
    bool canBeParsed = int.TryParse(time2var[0], out time2Intvar1);
    

    之后被初始化。

    【讨论】:

      猜你喜欢
      • 2017-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-24
      • 1970-01-01
      相关资源
      最近更新 更多