Public Class frmGrades
  Dim students(50) As Student
  Dim lastStudentAdded As Integer = -1   'Position in array of student
  '                                       most recently added

  Private Sub btnEnter_Click(ByVal sender As System.Object,
                             ByVal e As System.EventArgs) Handles btnEnter.Click
    lastStudentAdded += 1
    students(lastStudentAdded) = New Student
    students(lastStudentAdded).Name = txtName.Text
    students(lastStudentAdded).SocSecNum = mtbSSN.Text
    students(lastStudentAdded).Midterm = CDbl(txtMidterm.Text)
    students(lastStudentAdded).Final = CDbl(txtFinal.Text)
    'Clear text boxes
    txtName.Clear()
    mtbSSN.Clear()
    txtMidterm.Clear()
    txtFinal.Clear()
    txtName.Focus()
    MessageBox.Show("Student Recorded.")
  End Sub

  Private Sub btnDisplay_Click(ByVal sender As System.Object,
                               ByVal e As System.EventArgs) Handles btnDisplay.Click
    ReDim Preserve students(lastStudentAdded)
    Dim query = From pupil In students
                Select pupil.Name, pupil.SocSecNum, pupil.CalcSemGrade
    dgvGrades.DataSource = query.ToList
    dgvGrades.CurrentCell = Nothing
    dgvGrades.Columns("Name").HeaderText = "Student Name"
    dgvGrades.Columns("SocSecNum").HeaderText = "SSN"
    dgvGrades.Columns("CalcSemGrade").HeaderText = "Grade"
    ReDim Preserve students(50)
    txtName.Focus()
  End Sub

  Private Sub btnQuit_Click(ByVal sender As System.Object,
                            ByVal e As System.EventArgs) Handles btnQuit.Click
    Me.Close()
  End Sub
End Class     'frmGrades

Class Student
  Private m_midterm As Double
  Private m_final As Double

  Public Property Name() As String

  Public Property SocSecNum() As String

  Public WriteOnly Property Midterm() As Double
    Set(ByVal value As Double)
      m_midterm = value
    End Set
  End Property

  Public WriteOnly Property Final() As Double
    Set(ByVal value As Double)
      m_final = value
    End Set
  End Property

  Function CalcSemGrade() As String
    Dim grade As Double
    grade = (m_midterm + m_final) / 2
    grade = Math.Round(grade)  'Round the grade
    Select Case grade
      Case Is >= 90
        Return "A"
      Case Is >= 80
        Return "B"
      Case Is >= 70
        Return "C"
      Case Is >= 60
        Return "D"
      Case Else
        Return "F"
    End Select
  End Function
End Class     'Student
