Well offcourse - you are getting and setting properties recursively. For each property in your code you must have a variable to keep its value.
Here's the corrected source:
using System;
namespace Assignment1
{
public class Staff
{
// attributes
private string _LastName, _FirstName;
// properties
public string LastName // Read Only
{
get { return _LastName; }
}
public string GivenName // Read Only
{
get { return _FirstName; }
}
// parameter/ custom constructor
public Staff(string firstname, string surname)
{
_LastName = surname;
_FirstName = firstname;
}
public override string ToString()
{
return string.Format("{0}, {1}", _LastName, _FirstName);
}
}
//{
// public string LastName
// {
// get { return LastName; }
// set { LastName = value; }
// }
// public string FirstName
// {
// get { return FirstName; }
// set { FirstName = value; }
// }
// public Staff() { }
// public Staff(string first, string last)
// {
// LastName = last;
// FirstName = first;
// }
// public Staff(Staff aStaff)
// {
// FirstName = aStaff.FirstName;
// LastName = aStaff.LastName;
// }
// public override string ToString()
// {
// string tostring = LastName + ", " + FirstName;
// return tostring;
// }
//}
public class Department
{
private string _Code, _Name;
//Properties
public string Code //Read only
{
get { return _Code; }
}
public string Name //Read only
{
get { return _Name; }
}
public Department(string code, string name)
{
_Code = code;
_Name = name;
}
public override string ToString()
{
return string.Format("{0} {1}", _Code, _Name);
}
}
public class Teaching
{
public string UnitCode
{
get { return _unitCode; }
set { _unitCode = value; }
}
private string _unitCode;
public ushort Trimester
{
get { return _trimester; }
set { _trimester = value; }
}
private ushort _trimester;
public Staff TeachingStaff
{
get { return _staff; }
set { _staff = value; }
}
private Staff _staff;
public Department TeachingDepartment
{
get { return _department; }
set { _department = value; }
}
private Department _department;
public Teaching() { }
public Teaching(Staff _staff, Department _department, string unit, ushort trimester)
{
TeachingStaff = _staff;
TeachingDepartment = _department;
UnitCode = unit;
Trimester = trimester;
}
//public copyconstructor
public Teaching(Teaching aTeaching)
{
TeachingStaff = aTeaching.TeachingStaff;
TeachingDepartment = aTeaching.TeachingDepartment;
UnitCode = aTeaching.UnitCode;
Trimester = aTeaching.Trimester;
}
public override string ToString()
{
string tostring = TeachingStaff + " " + TeachingDepartment + " " + UnitCode + " " + Trimester;
return tostring;
}
}
//public string Code
//{
// get { return Code; }
// set { Code = value; }
//}
// public string Name
// {
// get { return Name; }
// set { Name = value; }
// }
// public Department() { }
// public Department(string code, string name)
// {
// Code = code;
// Name = name;
// }
// public Department(Department aDepartment)
// {
// Code = aDepartment.Code;
// Name = aDepartment.Name;
// }
// public override string ToString()
// {
// string tostring = Code + " " + Name;
// return tostring;
// }
//}
//public class Teaching
//{
// // attributes
// private string _UnitCode, _Trimester, _Staff, _Department;
// // properties
// public string UnitCode // Read Only
// {
// get { return _UnitCode; }
// }
// public string Trimester // Read Only
// {
// get { return _Trimester; }
// }
// public Staff staff
// {
// get { return staff; }
// set { staff = value; }
// }
// public Department Department
// {
// get { return Department; }
// }
// // parameter/ custom constructor
// public Teaching(string UnitCode, string Trimester, string Staff, string Department)
// {
// _UnitCode = UnitCode;
// _Trimester = Trimester;
// _Staff = Staff;
// _Department = Department;
// }
// public override string ToString()
// {
// return string.Format("{0}, {1}, {2} ,{3}", _UnitCode, _Trimester, Staff, Department);
// }
//}
//public string UnitCode
//{
// get { return UnitCode; }
// set { UnitCode = value; }
//}
//public ushort Trimester
//{
// get { return Trimester; }
// set { Trimester = value; }
//}
//public Staff Staff
//{
// get { return Staff; }
// set { Staff = value; }
//}
//public Department Department
//{
// get { return Department; }
// set { Department = value; }
//}
// public Teaching() { }
// public Teaching(Staff staff, Department department, string unit, ushort trimester)
// {
// Staff = staff;
// Department = department;
// UnitCode = unit;
// Trimester = trimester;
// }
// //public copyconstructor
// public Teaching(Teaching aTeaching)
// {
// Staff = aTeaching.Staff;
// Department = aTeaching.Department;
// UnitCode = aTeaching.UnitCode;
// Trimester = aTeaching.Trimester;
// }
// public override string ToString()
// {
// string tostring = Staff + " " + Department + " " + UnitCode + " " + Trimester;
// return tostring;
// }
//}
public class Program
{
public static int[] FindStaff(string first, string last, Teaching[] list)
{
int[] integerArray = new int
[list.Length];
int found = 0;
for (int counter = 0; counter < list.Length; counter = counter + 1)
{
integerArray[counter] = -1;
}
for (int counter = 0; counter < list.Length; counter = counter + 1)
{
if (list[counter].TeachingStaff.GivenName == first && list[counter].TeachingStaff.LastName == last)
{
integerArray[found] = counter;
found = found + 1;
}
}
return integerArray;
}
public static int[] FindDepartment(string code, Teaching[] list)
{
int[] integerArray = new int
[list.Length];
int found = 0;
for (int counter = 0; counter < list.Length; counter = counter + 1)
{
integerArray[counter] = -1;
}
for (int counter = 0; counter < list.Length; counter = counter + 1)
{
if (list[counter].TeachingDepartment.Code == code)
{
integerArray[found] = counter;
found = found + 1;
}
}
return integerArray;
}
public static string FormatTeaching(Teaching teaching)
{
string StringTeaching = " - " + teaching.TeachingStaff.LastName + ", " + teaching.TeachingStaff.GivenName + " in " + teaching.Trimester + " For " + teaching.UnitCode + "(" + teaching.TeachingDepartment + ")";
return StringTeaching;
}
public static void Retrieve(int[] found, Teaching[] list)
{
for (int i = 0; i < found.Length; i++)
{
if (found[i] != -1)
{
FormatTeaching(list[found[i]]);
//if (found
[list[i]] != -1)
//{
// FormatTeaching(list[found[i]]);
}
}
}
public static void Heading(string data)
{
Console.WriteLine(data);
Console.WriteLine("{0}", new string('=', data.Length));
}
public static void Main()
{
// Create the Departments
Department[] DepartmentList = new Department[] { new Department("BUS", "Business Studies"),
new Department("COMP", "Computing and Maths") };
// Display
Heading("\nStarting Department list:");
foreach (Department d in DepartmentList) Console.WriteLine(d);
// Create the Staffs
Staff[] StaffList = new Staff[] { new Staff("Alan", "Brown"), new Staff("John", "Snow"),
new Staff("Mark", "White"),
new Staff("Ray", "Bright"), new Staff("Steve", "Green") };
// Display
Heading("\nStarting Staff list:");
foreach (Staff s in StaffList) Console.WriteLine(s);
Teaching[] TeachingList = new Teaching[] { new Teaching(StaffList[1], DepartmentList[0], "BUS101", 0),
new Teaching(StaffList[0], DepartmentList[0], "BUS101", 1),
new Teaching(StaffList[2], DepartmentList[0], "BUS101", 1),
new Teaching(StaffList[0], DepartmentList[0], "BUS102", 2),
new Teaching(StaffList[1], DepartmentList[0], "BUS102", 2),
new Teaching(StaffList[2], DepartmentList[1], "COM101",2),
new Teaching(StaffList[3], DepartmentList[1], "COM101", 3),
new Teaching(StaffList[4], DepartmentList[0], "BUS107", 4),
new Teaching(StaffList[4], DepartmentList[1], "SIT102", 1),
new Teaching(StaffList[3], DepartmentList[1], "SIT102", 3) };
// Display
Heading("\nStaff Teaching list:");
for (int i = 0; i < StaffList.Length; i++)
{
if (StaffList[i] != null)
{
Console.WriteLine("\n{0}\n", StaffList[i]);
int[] found = FindStaff(StaffList[i].GivenName, StaffList[i].LastName, TeachingList);
Retrieve(found, TeachingList);
}
}
Heading("\nDepartment Teaching list:");
for (int i = 0; i < DepartmentList.Length; i++)
{
if (DepartmentList[i] != null)
{
Console.WriteLine("\n{0}\n", DepartmentList[i]);
int[] found = FindDepartment(DepartmentList[i].Code, TeachingList);
Retrieve(found, TeachingList);
}
}
}
}
}
// public static void Main()
// {
// Heading("Department");
// Console.WriteLine("\n");
// Department[] DepartmentList = new Department[]
// {
// new Department("BUS","Business Studies"),
// new Department("COMP","Computing and Maths")
// };
// {
// Heading("\nStarting DepartmentList:");
// foreach (Department d in DepartmentList) Console.WriteLine(d);
// }
// Staff[] StaffList = new Staff[]
// {
// new Staff("Alan","Brown"),
// new Staff("John","Snow"),
// new Staff("Mark","White"),
// new Staff("Ray","Bright"),
// new Staff("Steve","Green")
// };
// {
// Heading("\nStarting Staff list:");
// foreach (Staff s in StaffList) Console.WriteLine(s);
// }
// Teaching[] TeachingList = new Teaching[]
// {
// new Teaching(StaffList[1],DepartmentList[0],"BUS101",1),
// new Teaching(StaffList[0],DepartmentList[0],"BUS101",1),
// new Teaching(StaffList[2],DepartmentList[0],"BUS101",1),
// new Teaching(StaffList[0],DepartmentList[0],"BUS102",2),
// new Teaching(StaffList[1],DepartmentList[0],"BUS102",2),
// new Teaching(StaffList[2],DepartmentList[1],"COM101",2),
// new Teaching(StaffList[3],DepartmentList[1],"COM101",3),
// new Teaching(StaffList[4],DepartmentList[0],"BUS107",3),
// new Teaching(StaffList[4],DepartmentList[1],"SIT102",1),
// new Teaching(StaffList[3],DepartmentList[1],"SIT102",3),
// };
// {
// Heading("\nStaff Teaching list");
// for (int i = 0; i < StaffList.Length; i++)
// {
// if (StaffList[i] != null)
// {
// Console.WriteLine("\n{0}\n", TeachingList[i].Staff);
// int[] found = FindStaff(TeachingList[i].Staff.FirstName, TeachingList[i].Staff.LastName, TeachingList);
// Retrieve(found, TeachingList);
// }
// }
// Heading("\nDepartment Teaching List:");
// for (int i = 0; i < DepartmentList.Length; i++)
// {
// if (DepartmentList[i] != null)
// {
// Console.WriteLine("\n{0}\n", TeachingList[i].Department);
// int[] found = FindDepartment(TeachingList[i].Department.Code, TeachingList);
// Retrieve(found, TeachingList);
// }
// }
// }
// }
// }
//}