wiredlime2015 - 11 days ago 6x

ASP.NET (C#) Question

I am using Visual Studio 2015 with ASP.NET & C#. I have this calculation I am trying to implement into C# coding, but it says abs and min math functions are not there.

This is the code:

`private double angle(int h, int m)`

{

h = Convert.ToInt32(ddlHours.SelectedItem.Value);

m = Convert.ToInt32(ddlMinutes.SelectedItem.Value);

double hAngle = 0.5D * (h * 60 + m);

double mAngle = 6 * m;

double angle = Math.abs(hAngle - mAngle);

angle = Math.min(angle, 360 - angle);

return angle;

}

Ultimately, I want the answer to go to my label.

How do I get the abs and min function, or is there another way that still provides the accuracy of this calculation?

Answer

I would suggest a few things;

- C# is a case sensitive language. The right syntax of those methods are
`Math.Abs`

and`Math.Min`

- Depending on your structure of course but this method would be better as a static method since
*probably*it will not need an instance to calculate this angel value. - You don't assign your
`ddlHours.SelectedItem.Value`

and`ddlMinutes.SelectedItem.Value`

to your parameters inside of your method. In such a case, there will be difference between`angle(5, 30)`

and`angle(6, 40)`

method calls since both do the calculation based on those dropdownlist values. Parse your dropdownlist values to integer and call your

`angel`

method with those parameters.`static double angle(int h, int m) { double hAngle = 0.5D * (h * 60 + m); double mAngle = 6 * m; double angle = Math.Abs(hAngle - mAngle); return Math.Min(angle, 360 - angle); }`

and call this method as;

```
int h = Convert.ToInt32(ddlHours.SelectedItem.Value);
int m = Convert.ToInt32(ddlMinutes.SelectedItem.Value);
double angel = YourType.angel(h, m);
```

Source (Stackoverflow)

Comments