Hello I'm trying to add new grades to a text file using the button btnRECORD entered in the textbox txtRECORD. I'm using the button btnDISPLAY to calculate the average of the grades and the number of grades above average and display them in a listbox. But I can't seem to add new grades to the textfile with btnRECORD. (the textfile is empty)
Public Class frmGRADES
Dim temp() As String = IO.File.ReadAllLines("Exam.txt")
Dim grades(temp.Length - 1) As Double
Dim average As Double
Dim aboveAverage As Integer
Function avg(ByVal average As Double, ByVal aboveAverage As Integer)//function for average
For i As Double = 0 To grades.Length - 1
If grades(i) > average Then
aboveAverage = aboveAverage + 1
End If
Next
Return aboveAverage
End Function
Private Sub btnRECORD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRECORD.Click
ReDim Preserve grades(temp.Length - 1)
grades(temp.Length - 1) = CDbl(txtRECORD.Text)
IO.File.WriteAllLines("Exam.txt", grades) //write grades to textfile
aboveAverage = 0
End Sub
Private Sub btnDISPLAY_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDISPLAY.Click
lstRESULTS.Items.Clear()
For i As Integer = 0 To grades.Length - 1
grades(i) = CDbl(temp(i))
Next
average = grades.Average
lstRESULTS.Items.Add("The Average Score is: " & average)
aboveAverage = avg(average, aboveAverage)
lstRESULTS.Items.Add("Number of Grades above average is: " & aboveAverage)
aboveAverage = 0
End Sub
End Class
Oops your C# is showing :). Comments in VB are signified with a '
Your main problem appears to be casting an array of double to string. In the WriteAllLines method. Something like this should work better:
IO.File.WriteAllLines("Exam.txt", (From d In grades
Let str As String = d.ToString
Select str).ToArray)
Another thing I noticed, array indexes are integer but one of your for loops is declared with a double.
To use the return type of a function you should explicitly declare that type so that the compiler can recognize it.
Function avg(ByVal average As Double, ByVal aboveAverage As Integer) As Integer
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.