Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Objects Loops and Final Touches Static Members

Andrew Winkler
Andrew Winkler
37,739 Points

So I don't get why this is not working... It's a static method for the Pythagorean theorem.

Welp. Here's my code:

RightTriangle.cs
using System.math;

namespace Treehouse.CodeChallenges
{
    class RightTriangle
    {
        double a, b, c;
    }

    public static CalculateHypotenuse(double a, double b)
    {
        c = math.Sqrt(a * a + b * b);
        return c;
    }
}

1 Answer

Ryan Sheppard
Ryan Sheppard
2,866 Points

A few problems here...

  1. Your CalculateHypotenuse method isn't in a class -- methods cannot exist in a namespace.
  2. Your CalculateHypotenuse method doesn't have a return value -- you want it to return a double.
  3. Your using System.math; directive should read using System; -- Math is a class, not a namespace.
  4. math in your CalculateHypotenuse method should read Math.Sqrt -- note capitalized 'M'.
  5. Static methods cannot access instance members, so your CalculateHypotenuse method can't actually access the c field.

This is what you were probably going for:

using System;

namespace Treehouse.CodeChallenges
{
    class RightTriangle
    {
        public static double CalculateHypotenuse(double a, double b)
        {
            return Math.Sqrt(a * a + b * b);
        }
    }
}