Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '------------------------------------------------------------------------------------------
- ' Notice of My Copyright and Intellectual Property Rights
- '
- ' Any intellectual property contained within the program by Joseph L. Bolen remains the
- ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
- ' publish or provide such intellectual property to any other person or entity for any
- ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
- '
- ' Copyright © 2016. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Program Name: Student Class Demo
- '
- ' Author: Joseph L. Bolen
- ' Date Created: 04 MAR 2016
- '
- ' Description: Demonstrate the the basic concepts of a user defined class.
- '
- ' Documentation is at:
- ' App's screen image is at: http://imgur.com/d90zTp8 and
- ' http://imgur.com/FnCCAvr
- ' App's Visual Basic .NET code is at http://pastebin.com/nr6Qw8bD
- ' Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
- '-------------------------------------------------------------------------------------------
- Option Strict On
- Imports System.Text ' For StringBuilder class.
- Public Class StudentForm
- ' Class level (or instanciated) variables declared.
- Private studentCount As Integer = 0 ' Incremental counter.
- Private students(4) As Student ' Simple array of student datatype.
- 'Private students As New List(Of Student) ' List(Of T)
- ' Add new Student to students array.
- Private Sub AddStudentButton_Click(sender As Object, e As EventArgs) _
- Handles AddStudentButton.Click
- 'Instantiate a New student And assign values to its properties.
- Try
- Dim addStudent As New Student()
- addStudent.StudentId = studentCount + 1 ' Offset by one.
- addStudent.FirstName = FirstNameTextBox.Text
- addStudent.LastName = LastNameTextBox.Text
- ' Add student to the array.
- students(studentCount) = addStudent
- studentCount += 1 ' Increment counter
- DisplayStudentsButton.Enabled = True
- Catch ex As Exception
- MessageBox.Show(ex.Message, Me.Text,
- MessageBoxButtons.OK, MessageBoxIcon.Error)
- FirstNameTextBox.Focus()
- Exit Sub
- End Try
- ' If no more room in array, disable add button.
- If studentCount > students.GetUpperBound(0) Then
- AddStudentButton.Enabled = False
- End If
- ResetTextboxes(
- '' Using a List(Of T)
- 'Try
- ' Dim addStudent As New Student With {
- ' .StudentId = studentCount + 1,
- ' .FirstName = FirstNameTextBox.Text,
- ' .LastName = LastNameTextBox.Text}
- ' Add Student to List.
- ' students.Add(addStudent)
- ' studentCount += 1 ' Increment counter
- ' DisplayStudentsButton.Enabled = True
- 'Catch ex As Exception
- ' MessageBox.Show(ex.Message, Me.Text,
- ' MessageBoxButtons.OK, MessageBoxIcon.Error)
- ' FirstNameTextBox.Focus()
- ' Exit Sub
- 'End Try
- 'ResetTextboxes()
- End Sub
- ' Using the StringBuilder class, output summary of students.
- Private Sub DisplayStudentsButton_Click(sender As Object, e As EventArgs) _
- Handles DisplayStudentsButton.Click
- Dim output As New StringBuilder("Student Listing:" & vbNewLine & vbNewLine)
- For idx As Integer = 0 To studentCount - 1
- output.AppendLine(students(idx).ToString)
- Next
- '' Using a List(Of T)
- 'For Each student In students
- ' output.AppendLine(student.ToString)
- 'Next
- MessageBox.Show(output.ToString, Me.Text)
- FirstNameTextBox.Focus()
- End Sub
- Private Sub ClearButton_Click(sender As Object, e As EventArgs) _
- Handles ClearButton.Click
- ResetTextboxes()
- End Sub
- ' Clear Textboxes on form and set focus to first input control.
- Private Sub ResetTextboxes()
- FirstNameTextBox.Clear()
- LastNameTextBox.Clear()
- FirstNameTextBox.Focus()
- End Sub
- End Class
- '===============================================================================================
- Public Class Student
- '' Auto-implemented properties
- 'Public Property StudentId As Integer
- 'Public Property FirstName As String
- 'Public Property LastName As String
- Private studentIdValue As Integer
- Public Property StudentId() As Integer
- Get
- Return studentIdValue
- End Get
- Set(ByVal value As Integer)
- If value < 1 Then
- Throw New ArgumentOutOfRangeException("Student Id must be a positive whole number.")
- End If
- studentIdValue = value
- End Set
- End Property
- Private firstNameValue As String
- Public Property FirstName() As String
- Get
- Return firstNameValue
- End Get
- Set(ByVal value As String)
- If String.IsNullOrWhiteSpace(value) Then
- Throw New ArgumentNullException("First Name must have a value.")
- End If
- firstNameValue = value
- End Set
- End Property
- Private lastNameValue As String
- Public Property LastName() As String
- Get
- Return lastNameValue
- End Get
- Set(ByVal value As String)
- If String.IsNullOrWhiteSpace(value) Then
- Throw New ArgumentNullException("Last Name must have a value.")
- End If
- lastNameValue = value
- End Set
- End Property
- ' Default Constructor
- Public Sub New()
- MyBase.New
- End Sub
- ' Constructor overload
- Public Sub New(ByVal id As Integer)
- StudentId = id
- End Sub
- ' Constructor overload
- Public Sub New(ByVal id As Integer, ByVal fName As String, ByVal lName As String)
- StudentId = id
- FirstName = fName
- LastName = lName
- End Sub
- Public Overrides Function ToString() As String
- Return String.Format("Student ID: {0:D3} is {1} {2}", StudentId, FirstName, LastName)
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment