0

When trying to cast an int from an array string value in the following code;

   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();
                }
            }

        }
    }

I am getting the following error; Error 1 No overload for method 'TryParse' takes 1 argument

6
  • 1
    This isn't a cast, it's parsing and/or a conversion. And with Intellisense, why isn't it apparent to you that an additional argument is required? I don't quite get that. Commented Jan 6, 2013 at 1:42
  • I thought timeXvar[X] was the argument. Commented Jan 6, 2013 at 3:10
  • Additional argument. Commented Jan 6, 2013 at 4:02
  • Show me where you see the word Additional Commented Jan 6, 2013 at 5:01
  • It's in my first comment to your question, plain as day. Is this the same reason you missed the information about this argument in Intellisense? Commented Jan 6, 2013 at 20:07

2 Answers 2

4

Use it as

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

For more information see

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

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

Sign up to request clarification or add additional context in comments.

Comments

4

You need to provide the out parameter for int.TryParse:

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

It is initalized afterwards.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.