0

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?

krlzlx
  • 5,752
  • 14
  • 47
  • 55
wiredlime2015
  • 123
  • 3
  • 15

3 Answers3

2

c# is case sensitive

double angle = Math.Abs(hAngle - mAngle);
angle = Math.Min(angle, 360 - angle);
1

Use Intellisense. Its a capital A on Abs: https://msdn.microsoft.com/en-us/library/a4ke8e73(v=vs.110).aspx

MSDN is your friend if Intellisense isn't working for you. Put your cursor on the words abs and press CTRL+SPACE and it will correct it for you

LDJ
  • 6,896
  • 9
  • 52
  • 87
0

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);
Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364