user2672216
user2672216

Reputation: 59

Struggling with how methods and classes interact

I'm new to learning C# and am trying to implement the below code however, I am unable to do so, receiving the error "A namespace cannot directly contain members such as fields or methods".

namespace Grades
{
    public string LetterGrade
    {
        get
        {
            string result;
            if (RoundResult(AverageGrade) >= 90)
            {
                result = "A";
            }
            else if (RoundResult(AverageGrade) >= 80)
            {
                result = "B";
            }
            else if (RoundResult(AverageGrade) >= 70)
            {
                result = "C";
            }
            else
            {
                result = "F";
            }
            return result;
        }
    }

    private double RoundResult(double result)
    {
        double r;
        r = Math.Round(result);
        return r;
    }

    public class GradeStatistics
    {
        public float AverageGrade = 50;
        public float HighestGrade = 78;
        public float LowestGrade = 11;
    }
}

What I am trying to accomplish is to create a method called "RoundResult" which will round the "AverageGrade" result. I am merely doing this as an experiment to try and understand how methods interact with each other.

The biggest hurdle I am facing while learning C# is in regards to methods and classes, how to use them correctly, when to place them within an existing class or create there own separate class etc. If someone has any recommended resource that goes into extensive step by step detail on how to implement methods and classes, that would be greatly appreciated.

Edit: Thanks to Reputation Farmer and wazdev for their answers. I'd like to add an additional question....

Why is the "GradeStatistic" method a valid method to call the "AverageGrade" from within the same class yet my "RoundResult" method can not be within the same class?

Upvotes: 3

Views: 92

Answers (2)

wazdev
wazdev

Reputation: 353

This error message is occurring because you have two methods directly inside your namespace declaration- they need to be wrapped inside a class.

One possible solution is to create a "GradeCalculator" class and put your two methods inside that ... please note that this is not an optimal solution, but I've tried to modify as little as posssible:

namespace Grades
{
    public class GradeCalculator
    {
        public string LetterGrade
        {
            get
            {
                string result;
                if (RoundResult(GradeStatistics.AverageGrade) >= 90)
                {
                    result = "A";
                }
                else if (RoundResult(GradeStatistics.AverageGrade) >= 80)
                {
                    result = "B";
                }
                else if (RoundResult(GradeStatistics.AverageGrade) >= 70)
                {
                    result = "C";
                }
                else
                {
                    result = "F";
                }
                return result;
            }
        }

        private double RoundResult(double result)
        {
            double r;
            r = Math.Round(result);
            return r;
        }
    }

    public static class GradeStatistics
    {
        public static float AverageGrade = 50;
        public static float HighestGrade = 78;
        public static float LowestGrade = 11;
    }
}

Upvotes: 1

Reputation Farmer
Reputation Farmer

Reputation: 387

As the error says, namespace can't contain methods. You should put them inside a class:

namespace Grades
{
    public static class GradeUtil {
        public static string LetterGrade { ... }
        private static double RoundResult(double result) { ... }
    }

    public class GradeStatistics
    {
        public float AverageGrade = 50;
        public float HighestGrade = 78;
        public float LowestGrade = 11;
    }
}

Note the word static. It allows you to call a method without object instance. I.e. you can write GradeUtil.LetterGrade .... It's unclear, cut looks like this is what you intended.

Upvotes: 0

Related Questions