TigerV
TigerV

Reputation: 13

"Cannot convert from string to double"

Please can someone help me with the following problem. In the last line I get an error message stating that I

cannot convert from string to double

It works though in the first two lines. Help will be much appreciated.

DeltaY.Text = (Convert.ToDecimal(YCrd2.Text) - Convert.ToDecimal(YCrd1.Text)).ToString();

DeltaX.Text = (Convert.ToDecimal(XCrd2.Text) - Convert.ToDecimal(XCrd1.Text)).ToString();

Obs1Angle.Text = Math.Tan((Convert.ToDecimal(DeltaY.Text)) /
                          (Convert.ToDecimal(DeltaX.Text))).ToString();

Upvotes: 0

Views: 2247

Answers (4)

Mong Zhu
Mong Zhu

Reputation: 23732

When trying your code I get the error message:

CS1503 Argument 1: cannot convert from 'decimal' to 'double'

If you look at the documentation of Math.Tan you will see that it takes a double as input parameter and not a decimal! So you need a different conversion:

EDIT: Furthermore you chose the wrong method. It looks like you want an angle as result. In this case you need to use the Atan method which takes as Parameter:

A number representing a tangent.

and returns:

An angle, θ, measured in radians, such that -π/2 ≤θ≤π/2.

Obs1Angle.Text = Math.Atan((Convert.ToDouble(DeltaY.Text)) / (Convert.ToDouble(DeltaX.Text))).ToString();

Upvotes: 0

Mark Cilia Vincenti
Mark Cilia Vincenti

Reputation: 1614

Convert to Double and do it once.

var deltaY = Convert.ToDouble(YCrd2.Text) - Convert.ToDouble(YCrd1.Text);
var deltaX = Convert.ToDouble(XCrd2.Text) - Convert.ToDouble(XCrd1.Text);

DeltaY.Text = deltaY.ToString();
DeltaX.Text = deltaX.ToString();

Obs1Angle.Text = Math.Tan(deltaY /deltaX).ToString();

Much neater code, no?

Alternatively, if you want to go for parsing, using C# 7.0...

if (double.TryParse(YCrd2.Text, out double yCrd2) &&
    double.TryParse(YCrd1.Text, out double yCrd1) &&
    double.TryParse(XCrd2.Text, out double xCrd2) &&
    double.TryParse(XCrd1.Text, out double xCrd1))
{
    var deltaY = yCrd2 - yCrd1;
    var deltaX = xCrd2 - xCrd1;

    DeltaY.Text = deltaY.ToString();
    DeltaX.Text = deltaX.ToString();

    Obs1Angle.Text = Math.Tan(deltaY /deltaX).ToString();
}
else
{
    // show error in string to double conversion
}

PS: As @DmitryBychenko notes in a comment, use Math.Atan2 instead of Math.Tan if DeltaX.Text can be 0.

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

In order to detect wrong format, you can try TryParse methods (double.TryParse in your case)

double y1 = 0;
double x1 = 0;
double y2 = 0;
double x2 = 0;

if (!double.TryParse(YCrd1.Text, out y1)) 
  Obs1Angle.Text = "Incorrect YCrd1 value";
else if (!double.TryParse(YCrd2.Text, out y2)) 
  Obs1Angle.Text = "Incorrect YCrd2 value";
else if (!double.TryParse(XCrd1.Text, out x1)) 
  Obs1Angle.Text = "Incorrect XCrd1 value";
else if (!double.TryParse(XCrd2.Text, out x2)) 
  Obs1Angle.Text = "Incorrect XCrd2 value";
else {
  // All values (x1, x2, y1, y2) are parsed
  DeltaX.Text = $"{x2 - x1}";
  DeltaY.Text = $"{y2 - y1}";

  //DONE: If you want to compute Angle, you want Arc function (ASin, ATan, ACos etc.)
  // Please, note, Atan2 - in case x2 - x1 == 0
  Obs1Angle.Text = $"{Math.Atan2(y2 - y1, x2 - x1)}";
}

Upvotes: 1

Hien Nguyen
Hien Nguyen

Reputation: 18975

You should use TryParse for preventing your string is not number.

decimal DeltaYVal, DeltaXVal;
if (decimal.TryParse(DeltaY.Text, out DeltaYVal) && decimal.TryParse(DeltaY.Text, out DeltaXVal))
{
                Obs1Angle.Text = Math.Tan(DeltaYVal / DeltaXVal).ToString();
}

Upvotes: 0

Related Questions