public enum EmploymentStatus { FullTime, PartTime, Unknown }; public class Employee { private long emplNbr; private string name; private EmploymentStatus st; private double wage; public long EmployeeNumber { get { return emplNbr; } set { emplNbr = value; } } public string EmployeeName { get { return name; } set { name = value; } } public EmploymentStatus Status { get { return st; } set { st = value; } } public double HourlySalary { get { return wage; } set { wage = value; } } }
To create an array of objects, you can declare an array variable and use the square brackets to specify its size. Here is an example: public static class Exercise { static int Main(string[] args) { Employee[] StaffMembers = new Employee[3]; return 0; } } You can also use the var keyword to create the array but omit the first square brackets. Here is an example: public static class Exercise { static int Main(string[] args) { var StaffMembers = new Employee[3]; return 0; } }
If you create an array like this, you can then access each member using its index, allocate memory for it using the new operator, then access each of its fields, still using its index, to assign it the desired value. Here is an example: public static class Exercise { static int Main(string[] args) { var StaffMembers = new Employee[3]; StaffMembers[0] = new Employee(); StaffMembers[0].EmployeeNumber = 20204; StaffMembers[0].EmployeeName = "Harry Fields"; StaffMembers[0].Status = EmploymentStatus.FullTime; StaffMembers[0].HourlySalary = 16.85; StaffMembers[1] = new Employee(); StaffMembers[1].EmployeeNumber = 92857; StaffMembers[1].EmployeeName = "Jennifer Almonds"; StaffMembers[1].Status = EmploymentStatus.FullTime; StaffMembers[1].HourlySalary = 22.25; StaffMembers[2] = new Employee(); StaffMembers[2].EmployeeNumber = 42963; StaffMembers[2].EmployeeName = "Sharon Culbritt"; StaffMembers[2].Status = EmploymentStatus.PartTime; StaffMembers[2].HourlySalary = 10.95; return 0; } } As an alternative, you can also initialize each member of the array when creating it. To do this, before the semi-colon of creating the array, open the curly brackets, allocate memory for each member and specify the values of each field. To do this, you must use a constructor that takes each member you want to initialize, as argument. Here is an example: using System; public enum EmploymentStatus { FullTime, PartTime, Unknown }; public class Employee { private long emplNbr; private string name; private EmploymentStatus st; private double wage; public Employee() { } public Employee(long Number, string Name, EmploymentStatus EStatus, double Salary) { emplNbr = Number; name = Name; st = EStatus; wage = Salary; } public long EmployeeNumber { get { return emplNbr; } set { emplNbr = value; } } public string EmployeeName { get { return name; } set { name = value; } } public EmploymentStatus Status { get { return st; } set { st = value; } } public double HourlySalary { get { return wage; } set { wage = value; } } } public static class Exercise { static int Main(string[] args) { var StaffMembers = new Employee[] { new Employee(20204, "Harry Fields", EmploymentStatus.FullTime, 16.85), new Employee(92857, "Jennifer Almonds", EmploymentStatus.FullTime, 22.25), new Employee(42963, "Sharon Culbritt", EmploymentStatus.PartTime, 10.95) }; return 0; } } If using the var keyword and a constructor to initialize the array, you can omit calling the name of the class before the square brackets. Here is an example: public static class Exercise { static int Main(string[] args) { var StaffMembers = new[] { new Employee(20204, "Harry Fields", EmploymentStatus.FullTime, 16.85), new Employee(92857, "Jennifer Almonds", EmploymentStatus.FullTime, 22.25), new Employee(42963, "Sharon Culbritt", EmploymentStatus.PartTime, 10.95) }; return 0; } }
After creating and initializing the array, you can use it as you see fit. For example, you may want to display its values to the user. You can access any member of the array by its index, then use the same index to get its field(s) and consequently its (their) value(s). Here is an example: public static class Exercise { static int Main(string[] args) { var StaffMembers = new Employee[] { new Employee(20204, "Harry Fields", EmploymentStatus.FullTime, 16.85), new Employee(92857, "Jennifer Almonds", EmploymentStatus.FullTime, 22.25), new Employee(42963, "Sharon Culbritt", EmploymentStatus.PartTime, 10.95) }; Console.WriteLine("Employee Record"); Console.WriteLine("---------------------------"); Console.WriteLine("Employee #: {0}", StaffMembers[2].EmployeeNumber); Console.WriteLine("Full Name: {0}", StaffMembers[2].EmployeeName); Console.WriteLine("Status: {0}", StaffMembers[2].Status); Console.WriteLine("Hourly Wage {0}", StaffMembers[2].HourlySalary); Console.WriteLine("---------------------------\n"); return 0; } } This would produce: Employee Record --------------------------- Employee #: 42963 Full Name: Sharon Culbritt Status: PartTime Hourly Wage 10.95 --------------------------- Press any key to continue . . . Once again, remember that the index you use must be higher than 0 but lower than the number of members - 1. Otherwise, the compiler would throw an IndexOutRangeException exception. In the same way, you can use a for loop to access all members of the array using their index. Here is an example: public static class Exercise { static int Main(string[] args) { var StaffMembers= new Employee[] { new Employee(20204, "Harry Fields", EmploymentStatus.FullTime, 16.85), new Employee(92857, "Jennifer Almonds", EmploymentStatus.FullTime, 22.25), new Employee(42963, "Sharon Culbritt", EmploymentStatus.PartTime, 10.95) }; Console.WriteLine("Employees Records"); Console.WriteLine("=========================="); for (int i = 0; i < 3; i++) { Console.WriteLine("Employee #: {0}", StaffMembers[i].EmployeeNumber); Console.WriteLine("Full Name: {0}", StaffMembers[i].EmployeeName); Console.WriteLine("Status: {0}", StaffMembers[i].Status); Console.WriteLine("Hourly Wage {0}", StaffMembers[i].HourlySalary); Console.WriteLine("---------------------------"); } return 0; } } To access each member of the array, you can use the foreach operator that allows you to use a name for each member and omit the square brackets. Here is an example: public static class Exercise { static int Main(string[] args) { var StaffMembers = new Employee[] { new Employee(20204, "Harry Fields", EmploymentStatus.FullTime, 16.85), new Employee(92857, "Jennifer Almonds", EmploymentStatus.FullTime, 22.25), new Employee(42963, "Sharon Culbritt", EmploymentStatus.PartTime, 10.95) }; Console.WriteLine("Employees Records"); Console.WriteLine("=========================="); foreach(var Member in StaffMembers) { Console.WriteLine("Employee #: {0}", Member.EmployeeNumber); Console.WriteLine("Full Name: {0}", Member.EmployeeName); Console.WriteLine("Status: {0}", Member.Status); Console.WriteLine("Hourly Wage {0}", Member.HourlySalary); Console.WriteLine("---------------------------"); } return 0; } } This would produce: Employees Records ========================== Employee #: 20204 Full Name: Harry Fields Status: FullTime Hourly Wage 16.85 --------------------------- Employee #: 92857 Full Name: Jennifer Almonds Status: FullTime Hourly Wage 22.25 --------------------------- Employee #: 42963 Full Name: Sharon Culbritt Status: PartTime Hourly Wage 10.95 --------------------------- Press any key to continue . . .
Like a primitive type, an array of objects can be made a field of a class. You can primarily declare the array and specify its size. Here is an example: public class CompanyRecords { Employee[] Employees = new Employee[2]; } After doing this, you can then initialize the array from the index of each member. Alternatively, as we saw for field arrays of primitive types, you can declare the array in the body of the class, then use a constructor or another method of the class to allocate memory for it. To initialize the array, remember that each member is a value that must be allocated on the heap. Therefore, apply the new operator on each member of the array to allocate its memory, and then initialize it. Here is an example: public class CompanyRecords { Employee[] Employees; public CompanyRecords() { Employees = new Employee[2]; Employees[0] = new Employee(); Employees[0].EmployeeNumber = 70128; Employees[0].EmployeeName = "Frank Dennison"; Employees[0].Status = EmploymentStatus.PartTime; Employees[0].HourlySalary = 8.65; Employees[1] = new Employee(); Employees[1].EmployeeNumber = 24835; Employees[1].EmployeeName = "Jeffrey Arndt"; Employees[1].Status = EmploymentStatus.Unknown; Employees[1].HourlySalary = 16.05; } } If the class used as field has an appropriate constructor, you can use it to initialize each member of the array. Here is an example: public class CompanyRecords { Employee[] Employees; public CompanyRecords() { Employees = new Employee[] { new Employee(70128, "Justine Hearson", EmploymentStatus.PartTime, 10.62), new Employee(24835, "Bertha Hack", EmploymentStatus.FullTime, 18.94), new Employee(70128, "Frank Dennison", EmploymentStatus.Seasonal, 12.48), new Employee(24835, "Jeffrey Arndt", EmploymentStatus.PartTime, 16.05), }; } }
Once you have created and initialized the array, you can use it as you see fit, such as displaying its values to the user. You must be able to access each member of the array, using its index. Once you have accessed member, you can get to its fields or properties. Here is an example: using System; public enum EmploymentStatus { FullTime, PartTime, Seasonal, Unknown }; public class Employee { private long emplNbr; private string name; private EmploymentStatus st; private double wage; public Employee() { } public Employee(long Number, string Name, EmploymentStatus EStatus, double Salary) { emplNbr = Number; name = Name; st = EStatus; wage = Salary; } public long EmployeeNumber { get { return emplNbr; } set { emplNbr = value; } } public string EmployeeName { get { return name; } set { name = value; } } public EmploymentStatus Status { get { return st; } set { st = value; } } public double HourlySalary { get { return wage; } set { wage = value; } } } public class CompanyRecords { Employee[] Employees; public CompanyRecords() { Employees = new Employee[] { new Employee(70128, "Justine Hearson", EmploymentStatus.PartTime, 10.62), new Employee(24835, "Bertha Hack", EmploymentStatus.FullTime, 18.94), new Employee(70128, "Frank Dennison", EmploymentStatus.Seasonal, 12.48), new Employee(24835, "Jeffrey Arndt", EmploymentStatus.PartTime, 16.05), }; } public void ShowRecords() { Console.WriteLine("Employees Records"); Console.WriteLine("=========================="); foreach (var Member in Employees) { Console.WriteLine("Employee #: {0}", Member.EmployeeNumber); Console.WriteLine("Full Name: {0}", Member.EmployeeName); Console.WriteLine("Status: {0}", Member.Status); Console.WriteLine("Hourly Wage {0}", Member.HourlySalary); Console.WriteLine("---------------------------"); } } } public static class Exercise { static int Main(string[] args) { var Records = new CompanyRecords(); Records.ShowRecords(); return 0; } } This would produce: Employees Records ========================== Employee #: 70128 Full Name: Justine Hearson Status: PartTime Hourly Wage 10.62 --------------------------- Employee #: 24835 Full Name: Bertha Hack Status: FullTime Hourly Wage 18.94 --------------------------- Employee #: 70128 Full Name: Frank Dennison Status: Seasonal Hourly Wage 12.48 --------------------------- Employee #: 24835 Full Name: Jeffrey Arndt Status: PartTime Hourly Wage 16.05 --------------------------- Press any key to continue . . .
As done for an array of a primitive type, you can pass an array of objects as arguments. You follow the same rules we reviewed; that is, in the parentheses of a method, enter the class name, the empty square brackets, and the name of the argument. Here is an example: public class CompanyRecords { public CompanyRecords(Employee[] Employees) { } } You can then access each member of the argument and do what you judge necessary. For example, you can display the values that the argument is holding. To call a method that takes an array of objects, in its parentheses, just enter the name of the array. Here is an example: public class CompanyRecords { public CompanyRecords(Employee[] Employees) { Employees = new Employee[] { new Employee(70128, "Justine Hearson", EmploymentStatus.PartTime, 10.62), new Employee(24835, "Bertha Hack", EmploymentStatus.FullTime, 18.94), new Employee(70128, "Frank Dennison", EmploymentStatus.Seasonal, 12.48), new Employee(24835, "Jeffrey Arndt", EmploymentStatus.PartTime, 16.05), }; } } public static class Exercise { static int Main(string[] args) { var Values = new Employee[4]; CompanyRecords Records = new CompanyRecords(Values); Records.ShowRecords(); return 0; } } As stated for an array of primitive type, an array of objects passed as argument is treated as a reference. You can use this characteristic of arrays to initialize the array. You can also indicate that the array is passed by reference by preceding its name with the ref keyword. An array of objects can be returned from a method. To indicate this when defining the method, first type the name of the class followed by square brackets. Here is an example: public class CompanyRecords { public Employee[] RegisterEmployees() { } } In the body of the method, you can take care of any assignment you want. The major rule to follow is that, before exiting the method, you must return an array of the class indicated on the left side of the method name. To use the method, you can simply call it. If you want, since the method returns an array, you can retrieve that series and store it in a local array for later use. Here is an example: using System; public enum EmploymentStatus { FullTime, PartTime, Seasonal, Unknown }; public class Employee { private long emplNbr; private string name; private EmploymentStatus st; private double wage; public Employee() { } public Employee(long Number, string Name, EmploymentStatus EStatus, double Salary) { emplNbr = Number; name = Name; st = EStatus; wage = Salary; } public long EmployeeNumber { get { return emplNbr; } set { emplNbr = value; } } public string EmployeeName { get { return name; } set { name = value; } } public EmploymentStatus Status { get { return st; } set { st = value; } } public double HourlySalary { get { return wage; } set { wage = value; } } } public class CompanyRecords { public CompanyRecords() { } public Employee[] RegisterEmployees() { var Employees = new Employee[] { new Employee(70128, "Justine Hearson", EmploymentStatus.PartTime, 10.62), new Employee(24835, "Bertha Hack", EmploymentStatus.FullTime, 18.94), new Employee(70128, "Frank Dennison", EmploymentStatus.Seasonal, 12.48), new Employee(24835, "Jeffrey Arndt", EmploymentStatus.PartTime, 16.05), }; return Employees; } public void ShowRecords(ref Employee[] Records) { Console.WriteLine("Employees Records"); Console.WriteLine("=========================="); foreach (var Staff in Records) { Console.WriteLine("Employee #: {0}", Staff.EmployeeNumber); Console.WriteLine("Full Name: {0}", Staff.EmployeeName); Console.WriteLine("Status: {0}", Staff.Status); Console.WriteLine("Hourly Wage {0}", Staff.HourlySalary); Console.WriteLine("---------------------------"); } } } public static class Exercise { static int Main(string[] args) { var Records = new CompanyRecords(); var Contractors = Records.RegisterEmployees(); Records.ShowRecords(ref Contractors); return 0; } }
|
|||||||||||||||||||||||||||||||||||||||||||||