Domain Classes Chapter 9
You will learn to: 1. Code a Domain Class 2. Use the Domain Class within a Client Class (Form1) Student (UML) Fields -idNum -lastName -firstName -inGoodStanding -initials +COST_PER_HOUR -totalStudents +Student() +Student(studId, fn, ln) +GenEmailAddess +ReverseName() -UpdateInitials() +GetNamesStartingWith() Student Methods …which will instantiate objects 1111 Doe John JD true stu1 Doe John true JD
Domain Class Student (UML) Student Student - C# code Fields Methods public class Student { //Fields //Properties //Constructors //Methods } Student - C# code
Instance Fields Student (UML) Student Student - C# code stu1 stu2 1111 -idNum -lastName -firstName -inGoodStanding -initials Student Methods public class Student { //Fields--------------------------------------------- //Instance Fields (nonstatic) private int idNum; private string lastName; private string firstName; private bool inGoodStanding; private string initials; } Student - C# code stu1 stu2 1111 Doe John true JD 2222 Miller Sue false SM
Static Fields – i.e. at the Class level Student (UML) Fields -idNum -lastName -firstName -inGoodStanding -initials +COST_PER_HOUR -totalStudents Student Methods COST_PER_HOUR public class Student { //Fields---------------------------------------------- //Instance Fields (nonstatic) private int idNum; private string lastName; private string firstName; private bool inGoodStanding; private string initials; //Static Fields (i.e. class-level) public const double COST_PER_HOUR = 300.00; private static int totalStudents; } Student - C# code 300.00 totalStudents 2 1111 Doe John JD true 2222 Miller Sue SM false stu1 stu2 Doe John true JD Miller Sue false SM A note on Constants: Constants are static by default. C# does not support const properties. So, you must make the field public.
Properties Fields are private BUT we still need a way to give other classes access to the data The solution is Properties: Properties are similar to both: Like a Field – it can read/update data Like a Method – it can contain code Types of Properties Standard – explicitly written Auto-Implemented – the easy version public class Student { //Fields--------------------------------------- private int idNum; private string lastName; private string firstName; private bool inGoodStanding; private string initials; public const double COST_PER_HOUR = 300.00; private static int totalStudents; //Properties--------------------------------- } Student - C# code
Properties You have used Properties before…
Properties Student C# field Form property LastName { } lastName Student set get Form Last name: Smith Save get: fetches lastName: Doe set: updates lastName: Smith C# //Field private string lastName; //Property public string LastName { get { return lastName; } set { lastName = value; } } Standard Property - explicitly written
Properties – 2 approaches field property LastName { } lastName Standard Property - explicitly written set get C# Non-accessible Backing field Auto-Implemented Property //Field private string lastName; //Property public string LastName { get { return lastName; } set { lastName = value; } } //Field private string lastName; //Property public string LastName { get; set; } NOTE: Preferred way to get started!
Properties Standard Property Auto-implemented property Basic – get & set Basic – get & set public string LastName { get; set; } public string LastName { get { return lastName; } set { lastName = value; } } vs. Additional Code Additional Code public string LastName { get { return lastName; } set { lastName = value; UpdateInitials(); } } Cannot add additional code in an auto-implemented property vs. Read Only Read Only public string Initials { get { return initials; } } public string Initials { get; private set; } vs.
Writing Properties – Step 1: Try to make them all Auto-Implemented Student (UML) Fields -idNum -lastName -firstName -inGoodStanding -initials +COST_PER_HOUR -totalStudents Student Methods public class Student { //Fields---------------------------------------------- public const double COST_PER_HOUR = 300.00; //Properties----------------------------------------- //Constructors------------------------------------- //Methods------------------------------------------- } Student - C# code //Properties--------------------------------------------------- //Instance Properties public int IdNum { get; set; } public string LastName { get; set; } public string FirstName { get; set; } public bool InGoodStanding { get; set; } public string Initials { get; set; } //Static Properties public static int TotalStudents { get; set; }
Writing Properties – Step 2: Decide which ones require code or are read only Student (UML) Fields -idNum -lastName -firstName -inGoodStanding -initials +COST_PER_HOUR -totalStudents Student Methods public class Student { //Fields---------------------------------------------- private string lastName; private string firstName; public const double COST_PER_HOUR = 300.00; //Properties----------------------------------------- //Constructors------------------------------------- //Methods------------------------------------------- } Student - C# code //Properties--------------------------------------------------- //Instance Properties public int IdNum { get; set; } public string LastName get { return lastName; } set { lastName = value; UpdateInitials(); } public string FirstName get { return firstName; } set { firstName = value; UpdateInitials(); } public bool InGoodStanding { get; set; } public string Initials { get; private set; } //Static Properties public static int TotalStudents { get; set; } Update initials Update initials Read only
Constructors Student (UML) Student Student - C# code 4 totalStudents Fields -idNum -lastName -firstName -inGoodStanding -initials +COST_PER_HOUR -totalStudents +Student() +Student(studId, fn, ln) Student Methods public class Student { //Fields---------------------------------------------- private string lastName; private string firstName; public const double COST_PER_HOUR = 300.00; //Properties----------------------------------------- //Constructors------------------------------------- //Methods------------------------------------------- } Student - C# code //Constructors------------------------------------------------ public Student() InGoodStanding = true; TotalStudents++; public Student(int studId, string fname, string lname) IdNum = studId; FirstName = fname; LastName = lname; IdNum LastName –update Initials FirstName –update Initials InGoodStanding Initials -readonly TotalStudents -static 1111 Doe John JD true stu3 true 2222 Miller Sue SM false stu2 SM stu4 stu1 JD 4 totalStudents
Methods Student - C# code Student (UML) Student Fields Methods public class Student { //Fields---------------------------------------------- private string lastName; private string firstName; public const double COST_PER_HOUR = 300.00; //Properties----------------------------------------- //Constructors------------------------------------- //Methods------------------------------------------- } Student - C# code //Methods-------------------------------------------------- //Instance Methods private void UpdateInitials() if (FirstName != null && LastName != null) Initials = FirstName.Substring(0, 1) + LastName.Substring(0, 1); public string GenEmailAddress() string email = ""; if (FirstName != "") email = FirstName + "@abc.edu"; return email; public string ReverseName() string formattedName = LastName + ", " + FirstName; return formattedName; //Static Methods public static List<Student> GetNamesStartingWith() //see the code for this on the next page… IdNum LastName –update Initials FirstName –update Initials InGoodStanding Initials -readonly TotalStudents -static Student() Student(StudID, fname, lname) Student (UML) Fields -idNum -lastName -firstName -inGoodStanding -initials +COST_PER_HOUR -totalStudents +Student() +Student(studId, fn, ln) +GenEmailAddess +ReverseName() -UpdateInitials() +GetNamesStartingWith() Student Methods
Methods Student - C# code Student (UML) Student Fields Methods public class Student { //Fields---------------------------------------------- private string lastName; private string firstName; public const double COST_PER_HOUR = 300.00; //Properties----------------------------------------- //Constructors------------------------------------- //Methods------------------------------------------- } Student - C# code //Methods-------------------------------------------------- //Instance Methods private void UpdateInitials() if (FirstName != null && LastName != null) Initials = FirstName.Substring(0, 1) + LastName.Substring(0, 1); public string GenEmailAddress() string email = ""; if (FirstName != "") email = FirstName + "@abc.edu"; return email; public string ReverseName() string formattedName = LastName + ", " + FirstName; return formattedName; //Static Methods public static List<Student> GetNamesStartingWith() //see the code for this on the next page… IdNum LastName –update Initials FirstName –update Initials InGoodStanding Initials -readonly TotalStudents -static Student() Student(StudID, fname, lname) UpdateInitials() GenEmailAddress() ReverseName() GetNameStartWith() Student (UML) Fields -idNum -lastName -firstName -inGoodStanding -initials +COST_PER_HOUR -totalStudents +Student() +Student(studId, fn, ln) +GenEmailAddess +ReverseName() -UpdateInitials() +GetNamesStartingWith() Student Methods
Methods //Static Methods public static List<Student> GetNamesStartingWith(List<Student> students, string pattern) { int patternLen; List<Student> patternMatchStudents = new List<Student>(); patternLen = pattern.Length; foreach (Student aStudent in students) if (aStudent.lastName.Substring(0, patternLen) == pattern) patternMatchStudents.Add(aStudent); } return patternMatchStudents;
UI Design – with errors Class Instance Properties Methods Values produced via Methods Class Instance field but read-only Static field – not instance In red: intentionally showing mistakes in the UI Design. Some fields on the domain class are static fields & thus do not pertain to an individual student object). Some fields are read only & the client class cannot update its values.
Ch9Ex1 - Practice Coding a Domain Class Open project: Ch9Ex1-DomainClass Create the class called Student Code the Fields/Properties -code all fields in the UML Diagram as auto- implemented properties then decide if you need to change anything Id Num Last Name - when this is updated also execute UpdateInitials() First Name - when this is updated also execute UpdateInitials() In Good Standing - a boolean Initials - read only Cost Per Hour - a constant set to 300.00 Total Students - a static (i.e. class level) field Code the Constructors 1st Constructor - no parameters; sets In Good Standing to true in the new object & and increments Total Students 2nd Constructor - 3 parameters (student id, first name, last name); update these values in the new object & and increments Total Students Code the Methods UpdateInitials() - returns nothing but directly updates Initials if Last Name & First Name are not empty GenEmailAddress() - returns the email address (firstname@abc.edu) ReverseName() - returns a concatenated name: LastName + ", " + FirstName GetNamesStartingWith(list of students, pattern) - a "static" method that returns a list of students whose last name starts with the pattern provided – skip this method as it is not used in this form (but you might add it later for more practice) Student (UML) Fields -idNum -lastName -firstName -inGoodStanding -initials +COST_PER_HOUR -totalStudents +Student() +Student(studId, fn, ln) +GenEmailAddess +ReverseName() -UpdateInitials() +GetNamesStartingWith() Student Methods
Ch9Ex2 - Practice Using the Domain Class Client Class: Form1 Open project: Ch9Ex2-ClientClass Code the Submit button to utilize the Student class to do the following: Instantiate (i.e. create) a student object Fill the Object –with the data from the Form Display the object – in a label Re-Display the Total number of students (from the Student class) Code the Clear button to: Just invoke LoadInitialValues() This has the Domain class already coded