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.
C#
using System;
namespaceProgram
{
classUnit
{
// private attributesprivatestring _Code, _Name;
// propertiespublicstring Code
{
get{ return _Code; }
set { _Code = value; }
}
publicstring Name
{
get { return _Name; }
set { _Name = value; }
}
// constructorpublicUnit(string Code, string Name)
{
_Code = Code;
_Name = Name;
}
// override ToString methodpublicoverridestringToString()
{
returnstring.Format("{0} {1}", _Name, _Code);
}
}
classResult
{
// private attributesprivateint _Mark;
// propertiespublicint Mark
{
get { return _Mark; }
set { _Mark = value; }
}
publicstring Grade
{
get
{
string grade = "";
if (_Mark >= 80) grade = "HD";
elseif (_Mark >= 70) grade = "D";
elseif (_Mark >= 60) grade = "C";
elseif (_Mark >= 50) grade = "P";
elseif( _Mark >= 0) grade = "N";
return grade;
}
}
// constructorpublicResult(int Mark)
{
_Mark = Mark;
}
// override ToString methodpublicoverridestringToString()
{
returnstring.Format("{0} {1}", _Mark, Grade);
}
}
classEnrolment
{
// private attributesprivate Unit _Unit;
private Result _Result;
// propertiespublic Unit Unit
{
get { return _Unit; }
}
public Result Result
{
get { return _Result; }
set { _Result = value; }
}
publicEnrolment(Unit unit, Result result)
{
_Unit = unit;
_Result = result;
}
// override ToString methodpublicoverridestringToString()
{
returnstring.Format("\t{0}: {1}", _Result, _Unit);
}
}
classStudent
{
// private attributesprivatestring _ID, _FamilyName, _GivenName;
privateint _EnrolledMax = -1;
privateconstint MAX_UNITS = 4;
private Enrolment[] code = new Enrolment[MAX_UNITS];
// propertiespublic Enrolment this[string d]
{
get { return Enrol(d); }
}
publicstring ID
{
get { return _ID; }
set { _ID = value; }
}
publicstring FamilyName
{
get { return _FamilyName; }
set { _FamilyName = value; }
}
publicstring GivenName
{
get { return _GivenName; }
set { _GivenName = value; }
}
// constructorpublicStudent(string ID, string FamilyName, string GivenName)
{
_ID = ID;
_FamilyName = FamilyName;
_GivenName = GivenName;
}
publicvoidEnrol(Unit Unit)
{
this[Unit.Code] = new Enrolment(Unit);
//_EnrolledMax ++;
}
// override ToString methodpublicoverridestringToString()
{
returnstring.Format("ID {0}: {1}, {2}", _ID, _FamilyName, _GivenName);
}
}
classProgram
{
staticvoidMain()
{
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();
}
}
}
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Baimey RajeshRank #1,338 · 1.3K reputationPosted Aug 5, 2010, 3:58 AM EDT
✓ Accepted answer0
you are sending a string and the Enrol is acepting Unit.
Genrally Indexer sample is
class Client { private Dictionary invoices = new Dictionary();
Sasha B3 reputationPosted Aug 8, 2010, 11:12 PM EDT
0
Thanks Paru, I found your reply very helpful. Now my problem lies in assigning the object to the Enrolment Array on line 164 _EnrolledUnits[_EnrolledMax] = new Enrolment(code, null); I'm getting an error 'System.NullReferenceException: Object reference not set to an instance of an object.
0
Baimey RajeshRank #1,338 · 1.3K reputationPosted Aug 9, 2010, 12:44 AM EDT
Baimey RajeshRank #1,338 · 1.3K reputationPosted Aug 5, 2010, 3:58 AM EDT
Genrally Indexer sample is
class Client
{
private Dictionary
= new Dictionary
public void Add (int id, string value)
{ invoices.Add (id, value); }
public string this [int id] // indexer
{
get { return invoices[id]; }
set { invoices[id] = value; }
}
}
public class Test
{
public static void Main( )
{
Client client = new Client();
client.Add (1, "I005238A");
Console.WriteLine ("Invoice: " + client[1]); // indexer access
}
In ur sample
// properties
public Enrolment this[string d]
{
get { return Enrol(d); }
}
and
public void Enrol(Unit Unit)
{
this[Unit.Code] = new Enrolment(Unit);
//_EnrolledMax ++;
}
thanks
Paru
-- Mark if it helps
Sasha B3 reputationPosted Aug 8, 2010, 11:12 PM EDT
Now my problem lies in assigning the object to the Enrolment Array on line 164
_EnrolledUnits[_EnrolledMax] = new Enrolment(code, null);
I'm getting an error 'System.NullReferenceException: Object reference not set to an instance of an object.
Baimey RajeshRank #1,338 · 1.3K reputationPosted Aug 9, 2010, 12:44 AM EDT
This might be because Result is not a nullable object.
Please see http://msdn.microsoft.com/en-us/library/1t3y8s4s(VS.80).aspx
Thanks
Paru