0

I am relatively new in C# and I just want to get used in the language.

I have three classes namely Student, Course and Grade. I just can't seem to understand why it keeps getting a null value.

class Student
{
    Name name;
    Course course;

    public Student(Course course)
    {
        this.course = course;
    }

    public Course C
    {
        get { return course; }
        set { course = value; }
    }

}

class Course
{
    string name;
    string code;
    int units;
    Grade grade; // this one warns as 'Course.grade' is never assigned to, and will always have its default value null

    public Course(string name, string code)
    {
        this.name = name;
        this.code = code;
    }

    public void DisplayDetails()
    {
        Console.WriteLine("Course: {0}({1})", code, name);
    }

    public int U
    {
        get { return units; }
        set { units = value; }
    }

    public Grade G
    {
        get { return grade; }
        set { value = grade; }
    }
}

class Grade
{
    double q;
    double o;
    double fe;
    double fg;

    public Grade(double q, double o, double fe)
    {
        this.q = q;
        this.o = o;
        this.fe = fe;
    }

    public void ComputeGrade()
    {
        fg = q * .5 + o * .2 + fe * .3;
    }

    public double C
    {
        get { return fg; }
        set { value = fg; }
    }

}

And this is my main class.

class Program
{
    static void Main(string[] args)
    {
        Student s = new Student(new Course("Something", "1234"));
        s.C.U = 3;
        s.N = new Name("John", "Snow");
        Grade grade = new Grade(90.0, 90.0, 90.0); // I have initialized an object here


        s.C.G.ComputeGrade(); //but this gets a null value

        s.N.Display2();
        s.C.DisplayDetails();
        Console.WriteLine(" - {0} units", s.C.U);
        Console.WriteLine(" Grade: {0}", s.C.G.C); //as well as this
        Console.ReadKey();

    }
}

I believe to have set the values for an object of the Grade class. I have also declared it's getter/setter. Am I doing wrong with the code or lacking on the code?

5
  • 1
    Set G before use: s.C.G = new Grade(90.0, 90.0, 90.0);, then you can use it s.C.G.ComputeGrade(); Commented May 6, 2017 at 15:27
  • @Fabio As CodeFuller's answer rightly points out, that's not going to have any effect, because G's property setter effectively doesn't do anything. Commented May 6, 2017 at 15:28
  • Actually Fabio is also right, having correct setter is half of the job. But the setter is never called in the code. Commented May 6, 2017 at 15:30
  • That's fair enough, removed the "no" from my comment. Commented May 6, 2017 at 15:32
  • Thanks! My question has been answered. @Fabio Commented May 6, 2017 at 15:37

1 Answer 1

3

Correct G setter in Course class:

public Grade G
{
    get { return grade; }
    set { grade = value; }
}

and set G value before using like Fabio suggested:

s.C.G = new Grade(90.0, 90.0, 90.0);
Sign up to request clarification or add additional context in comments.

3 Comments

thank you! I didn't quite notice that, it removed the warning, but I am still getting the NullReferenceException on the method ComputeGrade().
Do you set value of G property as Fabio suggested?
Yes! Question has been answered! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.