The function getQuiz() accepts 2 parameters the line number and the path of a csv file. It splits out thhe question and options then checks against the correct answer which works fine. I want to be able to return the score at the end of the quiz. The problem lies with fact I'm looping through the function. The score resets each time.
How can I retrieve the score at the end of the quiz? This is how the function is being called:
'Calls the getQuiz function
Sub Main()
Dim i As Integer
For i = 2 To 6
getQuiz(i, "C:\Users\HisEasy.csv")
Next
End Sub
Function getQuiz(ByVal lineNumber As Integer, ByVal filename As String) As String
Dim allLines As List(Of String) = New List(Of String)
Dim qQuest, qOption1, qOption2, qOption3, answer
Dim quizVar
Dim n As Long
Dim score As long
score = 0
Using reader As New System.IO.StreamReader(filename)
For i As Integer = 1 To lineNumber - 1
If reader.ReadLine() Is Nothing Then
Throw New ArgumentOutOfRangeException("lineNumber")
End If
Next
' Attempts to read the line you've requested '
Dim line As String = reader.ReadLine()
If line Is Nothing Then
Throw New ArgumentOutOfRangeException("lineNumber")
End If
line = Replace(line, Chr(34), "")
quizVar = Split(line, ",")
qQuest = quizVar(1)
qOption1 = quizVar(2)
qOption2 = quizVar(3)
qOption3 = quizVar(4)
Console.WriteLine(qQuest)
Console.WriteLine(qOption1)
Console.WriteLine(qOption2)
answer = Console.ReadLine()
If answer = qOption3 Then
Console.WriteLine("Correct Answer!")
Console.ReadLine()
score = n + 1
Return score
End If
End Using
End Function
Mainpart. Simply return 0 or 1 as appropriate from thegetQuizfunction. Also, you may be interested in the File.ReadAllLines Method which reads all the lines from a file into an array in one go, so you wouldn't have to keep reading the file for every question.scorevariable outside thegetQuizmethod?