Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"No overload for method 'TryParse' takes one argument" error when using double.TryParse

Tags:

c#

Level:Novice

I'm writing a console program in C# that finds the area of circles, triangles, and trapezoids. I want to make sure the user only enters numbers, by using double.TryParse.

Here's the code:

        Console.WriteLine("AreaSolution finds the area of different shapes.");
        Console.ReadLine();
 ln1:   Console.WriteLine("To find area of circle, press c. To find area of triangle press t.For trapezoid, enter tr. ");

        var x = Console.ReadLine();
        switch (x)
        {
            case "c":
                Console.WriteLine("Enter radius of circle, in centimeters");
                var radius = Console.ReadLine();
                double rad = Convert.ToDouble(radius);

                if (double.TryParse(rad) == false)
                { Console.WriteLine("Numbers only!"); };


                Console.WriteLine("AREA: " + 3.14 * rad * rad + " cm sq.");
                Console.ReadLine();
                break;

            case "t":
                Console.WriteLine("Enter base, in centimeters");
                var bas = Console.ReadLine();
                double tbase = Convert.ToDouble(bas);


                Console.WriteLine("Enter height, in centimeters");
                var tHi = Console.ReadLine();
                double tHei = Convert.ToDouble(tHi);
                Console.WriteLine("AREA: " + (tbase * tHei / 2) + " cm sq.");
                Console.ReadLine();
                break;

            case "tr":
                Console.WriteLine("Enter the length of the top base, in cm");
                var tpbas = Console.ReadLine();
                double bas1 = Convert.ToDouble(tpbas);

                Console.WriteLine("Enter length of bottom base, in cm");
                var btmbase = Console.ReadLine();
                double bas2 = Convert.ToDouble(btmbase);

                Console.WriteLine("Enter height, in cm");
                var trHe = Console.ReadLine();
                double trH = Convert.ToDouble(trHe);

                Console.WriteLine("AREA: " + (bas1 + bas2) * trH / 2 + " cm sq.");
                Console.ReadLine();
                break;
            default:
                Console.WriteLine("Please enter a valid character:  c for circle, t for triangle, or tr for trapeziod.");
                break;


        }
        Console.WriteLine("Another computation?(Y/N)");
        string newComp = Console.ReadLine();

        switch (newComp)
        {
            case "y":
                goto ln1;
                break;
            case "n":
                return;
                break;
        }

However, on the line of "double.TryParse(rad)", I get an error:

No overload for method 'TryParse' takes one argument.

How can I fix this error? Thanks in advance. Your help is greatly appreciated.

like image 947
Edward Karak Avatar asked Jun 29 '13 02:06

Edward Karak


1 Answers

            double rad = Convert.ToDouble(radius);

            if (double.TryParse(rad) == false)

should be

            double rad;
            if (!double.TryParse(radius, out rad))

Use ! (not) instead of == false just as better coding practice.

Also, TryParse needs an out value (It does the parsing, no need for Convert.ToDouble and ... your Convert.ToDouble will error if it's not parseable - bad!)

like image 77
mcmonkey4eva Avatar answered Oct 09 '22 12:10

mcmonkey4eva