Struggling with how methods and classes interact

时间:2019-02-24 03:11:41

标签: c#

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?

2 个答案:

答案 0 :(得分:1)

发生此错误消息是因为您在名称空间声明中直接有两个方法-它们需要包装在一个类中。

一个可能的解决方案是创建一个“ GradeCalculator”类,并将其中的两个方法放入其中……请注意,这不是最佳解决方案,但我尝试了尽可能少的修改:

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;
    }
}

答案 1 :(得分:0)

如错误所示,名称空间不能包含方法。您应该将它们放在一个类中:

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;
    }
}

注意单词static。它允许您在没有对象实例的情况下调用方法。即您可以编写GradeUtil.LetterGrade ...。尚不清楚,切割看起来像您想要的那样。