Public Class frmGrades
  Dim pupil As Student  'pupil is an object of class Student

  Structure Person              'for use in btnDisplay_Close
    Dim name As String
    Dim socSecNum As String
    Dim semGrade As String
  End Structure

  Private Sub btnEnter_Click(ByVal sender As System.Object,
                             ByVal e As System.EventArgs) Handles btnEnter.Click
    pupil = New Student()   'Create an instance of the class Student
    'Read the values stored in the text boxes
    pupil.Name = txtName.Text
    pupil.SocSecNum = mtbSSN.Text
    pupil.Midterm = CDbl(txtMidterm.Text)
    pupil.Final = CDbl(txtFinal.Text)
    'Clear text boxes and list box
    txtName.Clear()
    mtbSSN.Clear()
    txtMidterm.Clear()
    txtFinal.Clear()
    'Notify user that grades for the student have been recorded
    MessageBox.Show("Student Recorded.")
  End Sub

  Private Sub btnDisplay_Click(ByVal sender As System.Object,
                               ByVal e As System.EventArgs) Handles btnDisplay.Click
    Dim persons(0) As Person
    persons(0).name = pupil.Name
    persons(0).socSecNum = pupil.SocSecNum
    persons(0).semGrade = pupil.CalcSemGrade
    Dim query = From someone In persons
                Select someone.name, someone.socSecNum, someone.semGrade
    dgvGrades.DataSource = query.ToList
    dgvGrades.CurrentCell = Nothing
    dgvGrades.Columns("name").HeaderText = "Student Name"
    dgvGrades.Columns("socSecNum").HeaderText = "SSN"
    dgvGrades.Columns("semGrade").HeaderText = "Grade"
  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_name As String       'Name
  Private m_ssn As String        'Social security number
  Private m_midterm As Double    'Numerical grade on midterm exam
  Private m_final As Double      'Numerical grade on final exam

  Public Property Name() As String
    Get
      Return m_name
    End Get
    Set(ByVal value As String)
      m_name = value
    End Set
  End Property

  Public Property SocSecNum() As String
    Get
      Return m_ssn
    End Get
    Set(ByVal value As String)
      m_ssn = value
    End Set
  End Property

  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



