C#尝试输入强制转换数组值

时间:2013-01-06 01:39:41

标签: c# tryparse

尝试从以下代码中的数组字符串值转换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个参数

2 个答案:

答案 0 :(得分:4)

将其用作

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

答案 1 :(得分:4)

您需要为out parameter提供int.TryParse

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

之后会进行初始化。