Sasha B

Sasha B

  • NA
  • 3
  • 2.9k

OOP and Indexer problem

Aug 5 2010 2:15 AM
Hi all,

I'm new to OOP and indexers. I need to create an indexer which accepts a string parameter (inside the Student class highlighted red) to store the Enrolment class object, however I get an error saying it can't convert from string type. And also having difficulty assigning the Enrolment object into the indexer.


using System;

namespace Program
{
      class Unit
      {
            // private attributes
            private string _Code, _Name;

            // properties
            public string Code
            {
                  get{ return _Code; }
                  set { _Code = value; }
            }
            public string Name
            {
                  get { return _Name; }
                  set { _Name = value; }
            }

            // constructor
            public Unit(string Code, string Name)
            {
                  _Code = Code;
                  _Name = Name;
            }

            // override ToString method
            public override string ToString()
            {
                  return string.Format("{0} {1}", _Name, _Code);
            }
      }

      class Result
      {
            // private attributes
            private int _Mark;

            // properties
            public int Mark
            {
                  get { return _Mark; }
                  set { _Mark = value; }
            }

            public string Grade
            {
                  get
                  {
                        string grade = "";
                        if (_Mark >= 80) grade = "HD";
                        else if (_Mark >= 70) grade = "D";
                        else if (_Mark >= 60) grade = "C";
                        else if (_Mark >= 50) grade = "P";
                        else if( _Mark >= 0) grade = "N";
                        return grade;
                  }
            }

            // constructor
            public Result(int Mark)
            {
                  _Mark = Mark;
            }

            // override ToString method
            public override string ToString()
            {
                  return string.Format("{0} {1}", _Mark, Grade);
            }
      }

      class Enrolment
      {
            // private attributes
            private Unit _Unit;
            private Result _Result;

            // properties
            public Unit Unit
            {
                  get { return _Unit; }
            }
            public Result Result
            {
                  get { return _Result; }
                  set { _Result = value; }
            }

            public Enrolment(Unit unit, Result result)
            {
                  _Unit = unit;
                  _Result = result;
            }

            // override ToString method
            public override string ToString()
            {
                  return string.Format("\t{0}: {1}", _Result, _Unit);
            }
      }

      class Student
      {
            // private attributes
            private string _ID, _FamilyName, _GivenName;
            private int _EnrolledMax = -1;
            private const int MAX_UNITS = 4;

            private
Enrolment[] code = new Enrolment[MAX_UNITS];

            // properties
            public Enrolment this[string d]
            {
                  get { return Enrol(d); }
            }
            public string ID
            {
                  get { return _ID; }
                  set { _ID = value; }
            }
            public string FamilyName
            {
                  get { return _FamilyName; }
                  set { _FamilyName = value; }
            }
            public string GivenName
            {
                  get { return _GivenName; }
                  set { _GivenName = value; }
            }

            // constructor
            public Student(string ID, string FamilyName, string GivenName)
            {
                  _ID = ID;
                  _FamilyName = FamilyName;
                  _GivenName = GivenName;
            }
            
            public void Enrol(Unit Unit)
            {
                  this[Unit.Code] = new Enrolment(Unit);
                  //_EnrolledMax ++;
            }

            // override ToString method
            public override string ToString()
            {
                  return string.Format("ID {0}: {1}, {2}", _ID, _FamilyName, _GivenName);
            }
      }
      class Program
      {
            static void Main()
            {
                  Unit u1 = new Unit("SIT103", "Introduction to Database Design");
                  Unit u2 = new Unit("SIT104", "Introduction to Web Development");
                  Unit u3 = new Unit("SIT131", "Object-Oriented Development");
                  Unit u4 = new Unit("SIT152", "Game Design");

                  Student demoStudent = new Student("210123456", "Smith", "J.");

                  demoStudent.Enrol(u1);
                  demoStudent.Enrol(u2);
                  demoStudent.Enrol(u3);
                  demoStudent.Enrol(u4);

                  Console.WriteLine(demoStudent);
                  Console.WriteLine(demoStudent["SIT103"]);
                  Console.WriteLine(demoStudent["SIT104"]);
                  Console.WriteLine(demoStudent["SIT131"]);
                  Console.WriteLine(demoStudent["SIT152"]);
                  Console.ReadKey();
             }
      }
}



Answers (3)