使用streamreader将数据从文本文件加载到文本框中,代码找不到objStudent数组

时间:2016-02-05 19:13:58

标签: vb.net

Option Strict On
Imports System.Text.RegularExpressions
Imports System.IO

Public Class StudentTestScores
    Private Structure Student
        Dim strStudentName As String
        Dim dblTestScores() As Double
        Dim dblAverage As Double
    End Structure

    Public Function GetDoubleTestScore(ByVal value As String) As Double
        'Checks if the value is numeric and returns message if error is found
        If IsNumeric(value) Then
            Dim dblValue = CDbl(value)
            'Check to make sure number is a positive and less or equal to 100
            If dblValue >= 0 And dblValue <= 100 Then
                Return dblValue
            Else
                Throw New Exception("The number needs to be between 0 and 100")
            End If
        Else
            Throw New Exception("Please enter a number in the test score area.")
        End If

    End Function
    Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
        'Creates variable and runs isValidName
        Dim objStudent(6) As Student
        If isValidName() = True Then

            Try
                ' This initializes each of the test score arrays in a Student object
                For i As Integer = 0 To 5
                    InitializeTestScores(objStudent(i))
                Next

                InitializeTestScores(objStudent(0))
                'runs isNumeric function to txtStudentScores
                objStudent(0).dblTestScores(0) = GetDoubleTestScore(txtStudent1Score1.Text)
                objStudent(0).dblTestScores(1) = GetDoubleTestScore(txtStudent1Score2.Text)
                objStudent(0).dblTestScores(2) = GetDoubleTestScore(txtStudent1Score3.Text)
                objStudent(0).dblTestScores(3) = GetDoubleTestScore(txtStudent1Score4.Text)
                objStudent(0).dblTestScores(4) = GetDoubleTestScore(txtStudent1Score5.Text)
                objStudent(1).dblTestScores(0) = GetDoubleTestScore(txtStudent2Score1.Text)
                objStudent(1).dblTestScores(1) = GetDoubleTestScore(txtStudent2Score2.Text)
                objStudent(1).dblTestScores(2) = GetDoubleTestScore(txtStudent2Score3.Text)
                objStudent(1).dblTestScores(3) = GetDoubleTestScore(txtStudent2Score4.Text)
                objStudent(1).dblTestScores(4) = GetDoubleTestScore(txtStudent2Score5.Text)
                objStudent(2).dblTestScores(0) = GetDoubleTestScore(txtStudent3Score1.Text)
                objStudent(2).dblTestScores(1) = GetDoubleTestScore(txtStudent3Score2.Text)
                objStudent(2).dblTestScores(2) = GetDoubleTestScore(txtStudent3Score3.Text)
                objStudent(2).dblTestScores(3) = GetDoubleTestScore(txtStudent3Score4.Text)
                objStudent(2).dblTestScores(4) = GetDoubleTestScore(txtStudent3Score5.Text)
                objStudent(3).dblTestScores(0) = GetDoubleTestScore(txtStudent4Score1.Text)
                objStudent(3).dblTestScores(1) = GetDoubleTestScore(txtStudent4Score2.Text)
                objStudent(3).dblTestScores(2) = GetDoubleTestScore(txtStudent4Score3.Text)
                objStudent(3).dblTestScores(3) = GetDoubleTestScore(txtStudent4Score4.Text)
                objStudent(3).dblTestScores(4) = GetDoubleTestScore(txtStudent4Score5.Text)
                objStudent(4).dblTestScores(0) = GetDoubleTestScore(txtStudent5Score1.Text)
                objStudent(4).dblTestScores(1) = GetDoubleTestScore(txtStudent5Score2.Text)
                objStudent(4).dblTestScores(2) = GetDoubleTestScore(txtStudent5Score3.Text)
                objStudent(4).dblTestScores(3) = GetDoubleTestScore(txtStudent5Score4.Text)
                objStudent(4).dblTestScores(4) = GetDoubleTestScore(txtStudent5Score5.Text)
                objStudent(5).dblTestScores(0) = GetDoubleTestScore(txtStudent6Score1.Text)
                objStudent(5).dblTestScores(1) = GetDoubleTestScore(txtStudent6Score2.Text)
                objStudent(5).dblTestScores(2) = GetDoubleTestScore(txtStudent6Score3.Text)
                objStudent(5).dblTestScores(3) = GetDoubleTestScore(txtStudent6Score4.Text)
                objStudent(5).dblTestScores(4) = GetDoubleTestScore(txtStudent6Score5.Text)

                ' This loops through each Student structure object and calculates the average test score.
                For i As Integer = 0 To 5
                    objStudent(i).dblAverage = CaculateStudentAverage(objStudent(i))
                Next

                objStudent(0).strStudentName = txtStudent1.Text
                objStudent(1).strStudentName = txtStudent2.Text
                objStudent(2).strStudentName = txtStudent3.Text
                objStudent(3).strStudentName = txtStudent4.Text
                objStudent(4).strStudentName = txtStudent5.Text
                objStudent(5).strStudentName = txtStudent6.Text

                lblAverageStudent1.Text = objStudent(0).dblAverage.ToString()
                lblAverageStudent2.Text = objStudent(1).dblAverage.ToString()
                lblAverageStudent3.Text = objStudent(2).dblAverage.ToString()
                lblAverageStudent4.Text = objStudent(3).dblAverage.ToString()
                lblAverageStudent5.Text = objStudent(4).dblAverage.ToString()
                lblAverageStudent6.Text = objStudent(5).dblAverage.ToString()
                'This creates the text file the program will write to
                Dim StudentFile As System.IO.StreamWriter
                Dim strFileName As String = "StudentTestScore.txt"
                StudentFile = System.IO.File.AppendText(strFileName)
                'Creates for loop that takes the 6 students
                For i As Integer = 0 To 5
                    StudentFile.Write("Student Name: ")
                    StudentFile.Write(objStudent(i).strStudentName)
                    StudentFile.Write("  Student Test Scores: ")


                    'This creates a loop for the students and test scores
                    For intIndex2 As Integer = 0 To 4
                        StudentFile.Write(objStudent(i).dblTestScores(intIndex2).ToString())
                        If intIndex2 <> 4 Then
                            StudentFile.Write(", ")
                        End If
                        'Finally the average is ran using the objStudent (i)
                    Next
                    StudentFile.Write("  Average Score = ")
                    StudentFile.Write(objStudent(i).dblAverage.ToString())
                    StudentFile.WriteLine()
                Next
                'Closes the text file that was created
                StudentFile.Close()
                'Shows a message box that says the file was written to the text file and or modified 
                MessageBox.Show("Student Test Score file was created or modified.")
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If
    End Sub
    Private Sub InitializeTestScores(ByRef objStudent As Student)    'references objStudent object          

        ' This takes the array dblTestScores and makes it a fixed array of size 6 since it could not be given a number in the structure 
        ReDim objStudent.dblTestScores(5)

    End Sub

    Private Function CaculateStudentAverage(ByVal objStudent As Student) As Double

        ' This loop loops through each value in dblTestScores and then just adds them to objstudent
        For i As Integer = 0 To 4
            objStudent.dblAverage += objStudent.dblTestScores(i)
        Next

        ' This divides and then stores it back into the variable
        objStudent.dblAverage /= 5
        'Returns student average 
        Return objStudent.dblAverage

    End Function

    Private Sub LoadToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LoadToolStripMenuItem.Click
        ' Create a new open file dialog
        Dim MyFileDialog As New System.Windows.Forms.OpenFileDialog
        ' Configure the dialog to show only text files
        ' Set its title and set the filename field blank for the moment.
        MyFileDialog.FileName = "StudentTestScore.txt"
        ' Show the dialog and see if the user pressed ok.
        If MyFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            ' Check to see if they selected a file and that it exists.
            If File.Exists(MyFileDialog.FileName) Then

                Dim strFile As String = MyFileDialog.FileName

                Dim reader As StreamReader
                Try
                    ' Setup a file stream reader to read the text file.
                    reader = New StreamReader(New FileStream(strFile, FileMode.Open, FileAccess.Read))
                    ' While there is data to be read, read each line into a rich edit box control.
                    Select

                        Case 0
                            txtStudent1.Text = Student.objStudent(0)
                            txtStudent1Score1.Text = 
                    End Select
                    While reader.Peek > -1
                        txtStudent1.Text &= reader.ReadLine()

                    End While
                    ' Close the file
                    reader.Close()
                Catch ex As FileNotFoundException
                    ' If the file was not found, tell the user.
                    MessageBox.Show("File was not found. Please try again.")
                End Try
            End If
        End If
    End Sub
End Class

0 个答案:

没有答案