JayBeeOH

Student Class Demo

Apr 20th, 2016
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.59 KB | None | 0 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2016. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   Student Class Demo
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   04 MAR 2016
  16. '
  17. ' Description:    Demonstrate the the basic concepts of a user defined class.
  18.  
  19. '
  20. '                 Documentation is at:
  21. '                   App's screen image is at: http://imgur.com/d90zTp8 and
  22. '                                               http://imgur.com/FnCCAvr
  23. '                   App's Visual Basic .NET code is at http://pastebin.com/nr6Qw8bD
  24. '                   Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
  25. '-------------------------------------------------------------------------------------------
  26.  
  27. Option Strict On
  28.  
  29. Imports System.Text                         ' For StringBuilder class.
  30.  
  31. Public Class StudentForm
  32.  
  33.     ' Class level (or instanciated) variables declared.
  34.     Private studentCount As Integer = 0     ' Incremental counter.
  35.     Private students(4) As Student          ' Simple array of student datatype.
  36.     'Private students As New List(Of Student) ' List(Of T)
  37.  
  38.     ' Add new Student to students array.
  39.     Private Sub AddStudentButton_Click(sender As Object, e As EventArgs) _
  40.         Handles AddStudentButton.Click
  41.  
  42.         'Instantiate a New student And assign values to its properties.
  43.         Try
  44.             Dim addStudent As New Student()
  45.             addStudent.StudentId = studentCount + 1     ' Offset by one.
  46.             addStudent.FirstName = FirstNameTextBox.Text
  47.             addStudent.LastName = LastNameTextBox.Text
  48.  
  49.             ' Add student to the array.
  50.             students(studentCount) = addStudent
  51.  
  52.             studentCount += 1   ' Increment counter
  53.             DisplayStudentsButton.Enabled = True
  54.         Catch ex As Exception
  55.             MessageBox.Show(ex.Message, Me.Text,
  56.                                 MessageBoxButtons.OK, MessageBoxIcon.Error)
  57.             FirstNameTextBox.Focus()
  58.             Exit Sub
  59.         End Try
  60.  
  61.         ' If no more room in array, disable add button.
  62.         If studentCount > students.GetUpperBound(0) Then
  63.             AddStudentButton.Enabled = False
  64.         End If
  65.  
  66.         ResetTextboxes(
  67.  
  68.         '' Using a List(Of T)
  69.         'Try
  70.         '    Dim addStudent As New Student With {
  71.         '        .StudentId = studentCount + 1,
  72.         '        .FirstName = FirstNameTextBox.Text,
  73.         '        .LastName = LastNameTextBox.Text}
  74.  
  75.         '   Add Student to List.
  76.         '    students.Add(addStudent)
  77.  
  78.         '    studentCount += 1   ' Increment counter
  79.         '    DisplayStudentsButton.Enabled = True
  80.         'Catch ex As Exception
  81.         '    MessageBox.Show(ex.Message, Me.Text,
  82.         '                    MessageBoxButtons.OK, MessageBoxIcon.Error)
  83.         '    FirstNameTextBox.Focus()
  84.         '    Exit Sub
  85.         'End Try
  86.  
  87.         'ResetTextboxes()
  88.     End Sub
  89.  
  90.     ' Using the StringBuilder class, output summary of students.
  91.     Private Sub DisplayStudentsButton_Click(sender As Object, e As EventArgs) _
  92.         Handles DisplayStudentsButton.Click
  93.  
  94.         Dim output As New StringBuilder("Student Listing:" & vbNewLine & vbNewLine)
  95.  
  96.         For idx As Integer = 0 To studentCount - 1
  97.             output.AppendLine(students(idx).ToString)
  98.         Next
  99.  
  100.         '' Using a List(Of T)
  101.         'For Each student In students
  102.         '    output.AppendLine(student.ToString)
  103.         'Next
  104.  
  105.         MessageBox.Show(output.ToString, Me.Text)
  106.         FirstNameTextBox.Focus()
  107.     End Sub
  108.  
  109.     Private Sub ClearButton_Click(sender As Object, e As EventArgs) _
  110.         Handles ClearButton.Click
  111.  
  112.         ResetTextboxes()
  113.     End Sub
  114.  
  115.     ' Clear Textboxes on form and set focus to first input control.
  116.     Private Sub ResetTextboxes()
  117.  
  118.         FirstNameTextBox.Clear()
  119.         LastNameTextBox.Clear()
  120.         FirstNameTextBox.Focus()
  121.     End Sub
  122.  
  123. End Class
  124.  
  125. '===============================================================================================
  126. Public Class Student
  127.  
  128.     '' Auto-implemented properties
  129.     'Public Property StudentId As Integer
  130.     'Public Property FirstName As String
  131.     'Public Property LastName As String
  132.  
  133.     Private studentIdValue As Integer
  134.     Public Property StudentId() As Integer
  135.         Get
  136.             Return studentIdValue
  137.         End Get
  138.         Set(ByVal value As Integer)
  139.             If value < 1 Then
  140.                 Throw New ArgumentOutOfRangeException("Student Id must be a positive whole number.")
  141.             End If
  142.             studentIdValue = value
  143.         End Set
  144.     End Property
  145.  
  146.     Private firstNameValue As String
  147.     Public Property FirstName() As String
  148.         Get
  149.             Return firstNameValue
  150.         End Get
  151.         Set(ByVal value As String)
  152.             If String.IsNullOrWhiteSpace(value) Then
  153.                 Throw New ArgumentNullException("First Name must have a value.")
  154.             End If
  155.             firstNameValue = value
  156.         End Set
  157.     End Property
  158.  
  159.     Private lastNameValue As String
  160.     Public Property LastName() As String
  161.         Get
  162.             Return lastNameValue
  163.         End Get
  164.         Set(ByVal value As String)
  165.             If String.IsNullOrWhiteSpace(value) Then
  166.                 Throw New ArgumentNullException("Last Name must have a value.")
  167.             End If
  168.             lastNameValue = value
  169.         End Set
  170.     End Property
  171.  
  172.     ' Default Constructor
  173.     Public Sub New()
  174.         MyBase.New
  175.     End Sub
  176.  
  177.     ' Constructor overload
  178.     Public Sub New(ByVal id As Integer)
  179.         StudentId = id
  180.     End Sub
  181.  
  182.     ' Constructor overload
  183.     Public Sub New(ByVal id As Integer, ByVal fName As String, ByVal lName As String)
  184.         StudentId = id
  185.         FirstName = fName
  186.         LastName = lName
  187.     End Sub
  188.  
  189.     Public Overrides Function ToString() As String
  190.         Return String.Format("Student ID: {0:D3} is {1} {2}", StudentId, FirstName, LastName)
  191.     End Function
  192. End Class
Advertisement
Add Comment
Please, Sign In to add comment