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
Asked
Active
Viewed 87 times
0

Ňɏssa Pøngjǣrdenlarp
- 38,411
- 12
- 59
- 178

HebrewHammer
- 11
- 2
-
1Where in that massive wall of code are we supposed to look? See [Ask] and [MCVE] – Ňɏssa Pøngjǣrdenlarp Feb 05 '16 at 19:18
-
Sorry first time using this site. I cut some parts out of it that seemed useless to show. I just want to make sure everyone can see the changes made throughout the process. I know somewhere in there it takes away being able to call objStudent later when I am trying to send it to textboxes. But pretty much the save function to a text file works fine. And then when i try to reference it again in the streamreader it will not let me. Sorry again for not knowing exactly how to use the site efficiently. – HebrewHammer Feb 05 '16 at 19:27
-
Same question: where is the error exactly and what is the exception or error message? – Ňɏssa Pøngjǣrdenlarp Feb 05 '16 at 19:31
-
It is kicking the error in the case(0) at the bottom of the code. It says 'objStudent' is not declared. It may be inaccessible due to its protection level. – HebrewHammer Feb 05 '16 at 19:40
-
`objStudent` is declared in btnCalc_click so it only exists in that procedure. It is an issue of Scope...[This answer should help you](http://stackoverflow.com/a/33249045/1070452) – Ňɏssa Pøngjǣrdenlarp Feb 05 '16 at 19:42
-
I somewhat understand how what you linked me would work without the Private structure. Do I need to do things differently since I am using a structure? – HebrewHammer Feb 05 '16 at 19:57
-
Private/Public/Friend are about the access level, not Scope. You need to Declare `objStudent` at the form level. – Ňɏssa Pøngjǣrdenlarp Feb 05 '16 at 20:01
-
Awesome I think that fixed it up! Thank you so much I am now a firm believer of the Stackoverflow Power! – HebrewHammer Feb 05 '16 at 20:03