I am writing a program that needs to search for specific range of numbers in am Array. The number that I need to search for are actually for grades that student would have received, ie As, Bs, Cs, Ds, Es, and Fs. This is my code:
Imports System.IO Imports System.Math
Public Class frmClassStatistics
'Arrays to hold the name and marks read from file
Private strName() As String
Private dblMark() As Integer
Private intNumStudents As Integer = 0
Private Average As Double
Private StandardDeviation As Double
Private arrHighestMark() As Double
Private Sub frmClassStatistics_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Reads each student name and final mark from the mark file. It them separates each name from the mark
'and places the student names and the final mark into two, one-dimensional arrays.
'One array will be an array of string (for the names) and the other array will be an array of real numbers (for the marks).
Dim strFilename As String
Dim i As Integer
Dim intLastBlank As Integer
Dim strInput As String
Dim StudentMarks = "C:\StudentMarks.txt"
strFilename = "StudentMarks.txt"
If File.Exists(strFilename) Then
Dim srReader As New StreamReader(strFilename)
Try
i = 0
While Not srReader.EndOfStream
strInput = srReader.ReadLine()
'find location of TAB between the name and the mark.
intLastBlank = strInput.IndexOf(vbTab)
'take all characters before the TAB and place them into the name array
strName(i) = strInput.Substring(0, intLastBlank)
intNumStudents = intNumStudents + 1
'take all characters after the TAB, convert them into a real number and
'place them into the mark array
dblMark(i) = Double.Parse(strInput.Substring(intLastBlank + 1))
i = i + 1
End While
Catch ex As Exception
MessageBox.Show("Problem reading data from the file")
End Try
srReader.Close() ' Close the file
End If
End Sub
Private Sub btnShowStatistics_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowStatistics.Click
'Finds the dblMark array to find the class avarage, highest Mark and number of students above the class avarage
'I earased it for simplicity
Grades()
End Sub
Private Sub Grades()
Dim blnFound As Boolean = False
Dim intCount As Integer = 0
Dim intPosition As Integer
' At the moment when I run the program I get an error here saying that a null Reference Exception was unhandled
Do While blnFound And intCount < dblMark.Length
If dblMark(intCount) >= 98 Then
blnFound = True
intPosition = intCount
End If
intCount += 1
Loop
'check to see if value found
If blnFound Then
txtAs.Text = intPosition
Else
txtAs.Text = 0
End If
End Sub
Am I complete off base here using this kind of loop? Is there some easier way to program this?