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?
s.C.G = new Grade(90.0, 90.0, 90.0);, then you can use its.C.G.ComputeGrade();G's property setter effectively doesn't do anything.