'______________________________________________________________________ ' CLASS INHERITANCE ' ' Ron Kessler & Company ' Created 8/6/06 for C# 2005 '______________________________________________________________________ 'The program demonstrates: ' 1. Using muliple custom classes ' 2. Form inheritance ' 3. How to add XML documentation ' 4. How to add Windows Help to your application 'This is the program we did in class. We create two classes: Person and Teacher 'Person has two properties and one method. It calculates the age of the person. 'We create a instructor class that inherits from the person class and we add the property of subject taught. 'When the program runs, it lets us enter the name of our myEmployee. If the person is a teacher it asks for the ' subject area they teach. 'It then computes the person's age. If a teacher, it also prints out what area they teach in a label. 'NOTES: 'Updated 8/6/06 ' shows how to use String.Concat to concatenate up to 4 strings in one call ' added checkbox for instructor status ' also uses the instructor class properties to fill the labels. This proves the instructor class does inherit ' the properties of the person class (name). 'IT SHOWS HOW TO USE XML DOCUMENTATION FOR YOUR CLASSES Public Class frmMain Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click Dim myEmployee As New Person 'new object called myEmployee based on the Person class I created Dim myTeacher As New Instructor Dim DOB As Date Dim Age As Integer = 0 '---now set the properties of my class myTeacher.FirstName = txtFirst.Text.ToUpper 'all caps myTeacher.LastName = txtLast.Text.ToUpper '---get the date of birth from the datepicker DOB = DatePicker.Value.Date '---calculate the person's age using my age method of the myEmployee object Age = myEmployee.Age(DOB) lblAge.Text = myTeacher.FirstName & " " & myTeacher.LastName & " is " & Age & " years old. " '---or use new string concatenation You can connect up to four strings in one call 'lblAge.Text = String.Concat(myEmployee.FirstName, " ", myEmployee.LastName) If chkTeacher.Checked Then myTeacher.SubjectArea = InputBox("What class do you teach?") lblAge.Text &= myTeacher.FirstName & " teaches " & myTeacher.SubjectArea End If End Sub Private Sub btnHelp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHelp.Click System.Diagnostics.Process.Start("class inheritance1.chm") End Sub Private Sub btnEnd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnd.Click Me.Close() End Sub End Class '__________________________________________________________ ' Person Class ' Created 8/6/06 for C# 2005 '__________________________________________________________ 'This class has two properties and one method (Age). Public Class Person '---module-level properties variables Private first As String Private last As String ''' ''' Sets the value for the First Name ''' Public Property FirstName() As String Get Return first End Get Set(ByVal Value As String) first = Value End Set End Property ''' ''' Sets the value for the First Name ''' ''' Public Property LastName() As String Get Return last End Get Set(ByVal Value As String) last = Value End Set End Property ''' ''' Computes the persons age from the DOB passed in the Age method ''' Public Function Age(ByVal DOB As Date) As Integer Return CInt(Now.Subtract(DOB).Days / 365) End Function End Class '__________________________________________________________ ' Instructor Class ' Created 8/6/06 for C# 2005 '__________________________________________________________ 'This class has one property. Public Class Instructor Inherits Person Private mySubjectArea As String ''' ''' Sets the value for the Subject area the instructor teaches. ''' Public Property SubjectArea() As String Get Return mySubjectArea End Get Set(ByVal Value As String) mySubjectArea = Value End Set End Property End Class