Imports Microsoft.VisualBasic
Imports System.Collections.Generic      ' List class
Imports System.Data                     ' In-memory data containers

Public Class StudentData
    Private Shared studentList As New List(Of Student)
    Private searchName As String

    Public Shared Sub TestData()
        ' Clear the student list otherwise we keep adding the same data
        '
        studentList.Clear()

        studentList.Add(New Student("Mike", 22, "WM"))
        studentList.Add(New Student("Kate", 23, "BIT"))
    End Sub

    Public Function SelectStudents() As List(Of Student)
        Return studentList
    End Function

    Public Sub InsertStudent(ByVal Name As String, ByVal Age As Integer, ByVal Course As String)
        studentList.Add(New Student(Name, Age, Course))
    End Sub

    Public Sub UpdateStudent(ByVal Key As String, ByVal Name As String, ByVal Age As Integer, ByVal Course As String)
        Dim foundStudent As Student
        searchName = Key
        foundStudent = studentList.Find(AddressOf FindStudent)
        If Not foundStudent Is Nothing Then
            foundStudent.Name = Name
            foundStudent.Age = Age
            foundStudent.Course = Course
        End If
    End Sub

    Public Sub DeleteStudent(ByVal Key As String)
        Dim foundStudent As Student
        searchName = Key
        foundStudent = studentList.Find(AddressOf FindStudent)
        If Not foundStudent Is Nothing Then
            studentList.Remove(foundStudent)
        End If
    End Sub

    Public Function FindStudent(ByVal theStudent As Student) As Boolean
        If searchName = theStudent.Key Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Sub InsertStudent2(ByVal theStudent As Student)
        studentList.Add(New Student(theStudent.Name, theStudent.Age, theStudent.Course))
    End Sub

    Public Function SelectStudents2() As DataTable
        Dim dTable As New DataTable("Students")
        Dim dRow As DataRow

        dTable.Columns.Add("Name", Type.GetType("System.String"))
        dTable.Columns.Add("Age", Type.GetType("System.Int32"))
        dTable.Columns.Add("Course", Type.GetType("System.String"))

        dRow = dTable.NewRow()
        dRow("Name") = "Mike"
        dRow("Age") = 22
        dRow("Course") = "WM"
        dTable.Rows.Add(dRow)

        dRow = dTable.NewRow()
        dRow("Name") = "Mark"
        dRow("Age") = 21
        dRow("Course") = "BIT"
        dTable.Rows.Add(dRow)

        Return dTable
    End Function

End Class
