Im working on Programming Challenge #4 in Chapter 8 of the textbook \"Starting O
ID: 3538184 • Letter: I
Question
Im working on Programming Challenge #4 in Chapter 8 of the textbook "Starting Out With Visual Basic 2010 fifth edition". I have already tried the the solution posted in the Textbook Help section here.
I get the error, "Reference to a non-shared member requires an object reference"
in the "Result.lstResult.Items.Clear()" line.
Question
The local Registry of Motor Vehicles office has asked you to create an application that grades the written portion of the driver's license exam. The exam has 20 multiple choice questions.Here are the correct answers to the questions.
1. B 6. A 11.B 16. C
2. D 7. B 12.C 17. C
3. A 8. A 13.D 18. B
4. A 9. C 14.A 19. D
5. C 10.D 15.D 20. A
Your application should store the correct answers in an array. A form should allow users to enter the answers to each question.When the user clicks the Score Exam button, the application should display another form showing whether each question was answered correctly or incorrectly, and whether the student passed of failed the exam. A student must correctly answer 15 of the 20 questions to pass the exam.
Input validation: Only accept the letters A,B,C, or D asanswers.
Here is the Code I have gotten:
Public Class Form1
Private Sub btnScore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScore.Click
Dim strGrades() As String = {"B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D", "C", "C", "B", "D", "A"}
Dim dtlResult(19) As Boolean
Dim intCorrect As Integer = 0
Dim validInput As Integer = 0
Result.lstResult.Items.Clear()
Dim intCount As Integer = 0
Dim strInput(19) As String
strInput(0) = txt1.Text
strInput(1) = txt2.Text
strInput(2) = txt3.Text
strInput(3) = txt4.Text
strInput(4) = txt5.Text
strInput(5) = txt6.Text
strInput(6) = txt7.Text
strInput(7) = txt8.Text
strInput(8) = txt9.Text
strInput(9) = txt10.Text
strInput(10) = txt11.Text
strInput(11) = txt12.Text
strInput(12) = txt13.Text
strInput(13) = txt14.Text
strInput(14) = txt15.Text
strInput(15) = txt16.Text
strInput(16) = txt17.Text
strInput(17) = txt18.Text
strInput(18) = txt19.Text
strInput(19) = txt20.Text
Try
For intCount = 0 To (strGrades.Length - 1)
If strInput(intCount).ToUpper() = "A" Or strInput(intCount).ToUpper() = "B" Or strInput(intCount).ToUpper() = "C" Or strInput(intCount).ToUpper() = "D" Then
If strGrades(intCount) = strInput(intCount).ToUpper() Then
intCorrect += 1
dtlResult(intCount) = True
End If
validInput = validInput + 1
End If
Next intCount
Catch ex As Exception
End Try
If validInput = 20 Then
Dim frmResult As New Result
frmResult.lstResult.Items.Clear()
frmResult.lstResult.Items.Add("-----------------------------------------------")
frmResult.lstResult.Items.Add("Driver's License Exam Results")
frmResult.lstResult.Items.Add("-----------------------------------------------")
For intCount = 0 To (19)
If (dtlResult(intCount) = True) Then
frmResult.lstResult.Items.Add(intCount + 1 & " Correct" & " Answer: " & strGrades(intCount))
Else
frmResult.lstResult.Items.Add(intCount + 1 & " Wrong" & " Answer: " & strGrades(intCount))
End If
Next intCount
frmResult.lstResult.Items.Add("-----------------------------------------------")
frmResult.lstResult.Items.Add(" Total :" & intCorrect & " /20")
If (intCorrect >= 15) Then
frmResult.lstResult.Items.Add("Your Result :" & " PASS")
Else
frmResult.lstResult.Items.Add("Your Result :" & " FAIL")
End If
frmResult.ShowDialog()
Else
MessageBox.Show("Input Should be A, B, C or D", "Input Validation")
End If
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
txt1.Text = String.Empty
txt2.Text = String.Empty
txt3.Text = String.Empty
txt4.Text = String.Empty
txt5.Text = String.Empty
txt6.Text = String.Empty
txt7.Text = String.Empty
txt8.Text = String.Empty
txt9.Text = String.Empty
txt10.Text = String.Empty
txt11.Text = String.Empty
txt12.Text = String.Empty
txt13.Text = String.Empty
txt14.Text = String.Empty
txt15.Text = String.Empty
txt16.Text = String.Empty
txt17.Text = String.Empty
txt18.Text = String.Empty
txt19.Text = String.Empty
txt20.Text = String.Empty
'or we can use simple way to clear all textbox controls within the groupbox
'For Each cntrl As Control In GroupBox1.Controls
' If TypeOf cntrl Is TextBox Then
' CType(cntrl, TextBox).Text = String.Empty
' End If
'Next cntrl
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub Label21_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub txt1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt1.TextChanged
End Sub
End Class
Explanation / Answer
Public Class frmJPDLExamm Dim intMinutes As Integer 'Holds the minutes for the timer Private Sub btnScore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScore.Click Dim AnswersForm As New frmAnswers 'Correct answers as an array Dim achrCorrectAnswers() As Char = {"B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D", "C", "C", "B", "D", "A"} Dim intIncorrect As Integer 'Counter for incorrect answers Dim intCorrect As Integer 'Counter for correct answers Dim intCount As Integer 'Total counter Dim chrInput As Char 'Hold a char value Dim achrUserAnswers(19) As Char 'Textboxes as an array achrUserAnswers(0) = txt1.Text achrUserAnswers(1) = txt2.Text achrUserAnswers(2) = txt3.Text achrUserAnswers(3) = txt4.Text achrUserAnswers(4) = txt5.Text achrUserAnswers(5) = txt6.Text achrUserAnswers(6) = txt7.Text achrUserAnswers(7) = txt8.Text achrUserAnswers(8) = txt9.Text achrUserAnswers(9) = txt10.Text achrUserAnswers(10) = txt11.Text achrUserAnswers(11) = txt12.Text achrUserAnswers(12) = txt13.Text achrUserAnswers(13) = txt14.Text achrUserAnswers(14) = txt15.Text achrUserAnswers(15) = txt16.Text achrUserAnswers(16) = txt17.Text achrUserAnswers(17) = txt18.Text achrUserAnswers(18) = txt19.Text achrUserAnswers(19) = txt20.Text Try 'Convert the user input from a textbox to a char strInput = CChar(achrUserAnswers(19)) For intCount = 0 To 19 'If the user's answer from the textbox is equals to the correct answer 'from the corresponding array, then the answer is correct If achrUserAnswers(19) = achrCorrectAnswers(19) Then intCorrect += 1 AnswersForm.lstAns.Items.Add("Correct") 'Else, the user's answer is incorrect Else intIncorrect += 1 AnswersForm.lstAns.Items.Add("Incorrect") intCount += 1 End If Next Catch 'Error message for invalid input MessageBox.Show("Enter letter A, B, C, or D only.") End Try 'Display the total incorrect answers in the listbox AnswersForm.lstAns.Items.Add("You have a total of " & intIncorrect.ToString() & " Incorrect answers.") intCorrect = 20 - intIncorrect 'Display the total correct answers in the listbox AnswersForm.lstAns.Items.Add("and a total of " & intCorrect.ToString() & " Correct answers.") 'If the user has 15 or more correct answers, then the user pass the exam If intCorrect > 14 Then AnswersForm.lstAns.Items.Add("Congratulations! You have passed the Driver's License Exam.") 'Else, the user fail the exam Else AnswersForm.lstAns.Items.Add("You have failed the Driver's License Exam. Try Again.") End If 'Display the results in another form AnswersForm.ShowDialog() End Sub Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click 'Clears all textboxes. From textbox 1 to 20. txt1.Clear() txt2.Clear() txt3.Clear() txt4.Clear() txt5.Clear() txt6.Clear() txt7.Clear() txt8.Clear() txt9.Clear() txt10.Clear() txt11.Clear() txt12.Clear() txt13.Clear() txt14.Clear() txt15.Clear() txt16.Clear() txt17.Clear() txt18.Clear() txt19.Clear() txt20.Clear() End Sub