2 Replies - 4679 Views - Last Post: 03 October 2011 - 07:36 PM Rate Topic: -----

#1 AVReidy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Cannot implicitly convert double to int, but an explicit conv. exists.

Posted 03 October 2011 - 07:16 PM

I've neglected getting to know all of the data types, parsing, math and whatnot in C#, so I decided to make a little command line calculator which includes a quadratic formula solver. My variables were obtained/input as strings and then converted to doubles (I think). I used the same method that converts strings to integers for this, but guessed on the syntax. So I wrote:

double a = double.Parse(sA);


I'm not sure if this is right, but I'm not getting any errors from it in Visual Studio.

Here's an excerpt from one class (I'm practicing with methods...):

                if (choice.Equals("Quadratic"))
                {
                    Console.WriteLine("");
                    Console.WriteLine("Quadratic Equation Solver");
                    Console.WriteLine("");
                    Console.WriteLine("Equation format:  ax^2 + bx + c = 0");
                    Console.WriteLine("");
                    Console.WriteLine("Input A: ");
                    string sA = Console.ReadLine();
                    Console.WriteLine("Input B: ");
                    string sB = Console.ReadLine();
                    Console.WriteLine("Input C: ");
                    string sC = Console.ReadLine();
                    double a = double.Parse(sA);
                    double b = double.Parse(sB);
                    double c = double.Parse(sC);

                    double solution1 = functions.Quadratic1(a, b, c);

                    Console.WriteLine(solution1);

                }



And here's the method in a different class that does the calculating:

        public int Quadratic1(double A, double B, double C)
        {
            double toBeSqrt = (B*B)/> - 4 * A * C;
            double sqrted = Math.Sqrt(toBeSqrt);
            double solution = -B + sqrted / 2 * A;
            return solution;
        }



Above, where it says "return solution," there is an error saying: "Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists. (Are you missing a cast?)."

It's telling me that I've explicitly converted double to int, but I'm probably missing a cast. It would probably be helpful to know what a cast is.

Thanks for the help!

Is This A Good Question/Topic? 0
  • +

Replies To: Cannot implicitly convert double to int, but an explicit conv. exists.

#2 GeorgeE  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 8
  • Joined: 01-October 11

Re: Cannot implicitly convert double to int, but an explicit conv. exists.

Posted 03 October 2011 - 07:33 PM

You are getting that error because you are trying to return a double from a method which got return type of int.

This should fix your problem:

// The method's return type is now double.
public double Quadratic1(double A, double B, double C)
{
    double toBeSqrt = (B*B)/> - 4 * A * C;
    double sqrted = Math.Sqrt(toBeSqrt);
    double solution = -B + sqrted / 2 * A;
    return solution;
}


This post has been edited by GeorgeE: 03 October 2011 - 07:38 PM

Was This Post Helpful? 2
  • +
  • -

#3 AVReidy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Re: Cannot implicitly convert double to int, but an explicit conv. exists.

Posted 03 October 2011 - 07:36 PM

Aha! Thanks for catching that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1