I need to add a private reference to a Unit object _Unit. Attached is both classes (Unit and Result).
I understand I need to have the code below, however it results in errors (listed below):
// 14. create new class
class Result : Unit
and the below creates the error that base() requires two constructors:
// 17. Create constructor for the class
public Result(string grade, Unit _Unit) : base(_Unit)
In my unit class there are two private strings _Code and _Name. Please ask if you need any other class codes put in or the assignment question.
namespace SIT232_Assignment1
{
// 14. create new class
class Result
{
// 15. Add a private reference to a Unit objectand a private string attributes.
private string _Grade, _Unit;
// 16. Encapsulate the above attributes with public read-only properties
public string Grade
{
get { return _Grade; }
}
// 17. Create constructor for the class
public Result(string grade, Unit _Unit)
{
_Grade = grade;
}
// 18. create a public read-only property of type bool
public bool Passed (string grade)
{
bool result = true;
if (_Grade == "N")
result = false;
return result;
}
// 19. Create a public static methods
public static bool ValidateGrade(string grade)
{
bool result = false;
if (_Grade == "N" || _Grade == "P" || _Grade == "C" || _Grade == "D" || _Grade =="HD")
result = true;
return result;
}
// 20. Define a ToString method
public override string ToString()
{
return String.Format("{0}\t{1}", _Grade);
}
}
namespace SIT232_Assignment1
{
// 8. Create new class
class Unit
{
// 9. Add private string attributes for the unit code and unit name
private string _Code, _Name;
// 10. Encapsulate the above attributes with public read-only properties.
public string Code
{
get { return _Code; }
}
public string Name
{
get { return _Name; }
}
// 11. Create constructor with two string parameters
public Unit( string code, string name)
{
_Code = code;
_Name = name;
}
// 27. create a private list<>
private List<Student> _EnrolledStudents = new List<Student>();
// 28. Encapsulate the above list with read-only
public ReadOnlyCollection<Student> EnrolledStudents
{
get { return _EnrolledStudents.AsReadOnly(); }
}
// 29. Create a method that accecpts a single parameter
public void RecordEnrollment(Student student)
{
_EnrolledStudents.Add(student);
}
// 30. Create a method that accecpts a single parameter
public void RemoveEnrollment(Student student)
{
_EnrolledStudents.Remove(student);
}
// 12. Define a ToString method
public override string ToString()
{
return String.Format("{0} {1}", _Code, _Name);
}
}
Furthermore, one other error I’m getting that I simply cannot full understand is the below method HAS to be static, I’ve researched that also making the attributes and properties of _Grade static solves the error showing on each individual _Grade, however it still shows on the first one?
if (_Grade == "N" || _Grade == "P" || _Grade == "C" || _Grade == "D" || _Grade =="HD")
public static bool ValidateGrade(string grade)
For your first question:
Your class
Resultinherites fromUnitand your constructor ofResultcalls the one of the base class. However, inUnitthere is only one constructor defined needing two arguments (codeandname), so your call tobaseat theResultconstructor needs to have two parameters.But you probably don’t want to inherit from
Unitbut add a private reference to it. There you would have something likeYour second error: A static method can only access static fields and properties, so from a static method you can’t access your instance variables. You just want to verify that the supplied
gradeis in your range, so just dont refer to the instance variable_Grade`: