Question Displaying/Calculating the score in a test.

Yhym

Member
Joined
Dec 8, 2013
Messages
7
Programming Experience
Beginner
Hi, I've got a problem with my spelling program where I can't display the correct score at the end of the test. Well, I can't actually display the score at all... Instead, it just displays the actual answers which is not what I want.
Btw. I'm using Access to store all the data if you wonder.
This is the code that I've so far:


VB.NET:
Public Class frmTest
    Dim Conn As New System.Data.OleDb.OleDbConnection()
    Dim Score As Integer = 0
    Dim Ansrs(1, 9) As String
    Dim r As Integer = 1
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles    btnClose.Click
        ' When a user will press the exit button, a message box will appear asking whether they want to close       the program and if a user clicks 'Yes' then 
        ' the whole program will close but if they click 'No' then the message box will simply dissapear 
        Dim Result As DialogResult
        Result = MessageBox.Show("Are you sure you wish to close the program?", "Close program?", MessageBoxButtons.YesNo)
        If Result = Windows.Forms.DialogResult.Yes Then
            ' Makes sure that the program will actually fully close
            Application.Exit()
        Else
        End If
    End Sub


    Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
        ' Instead of just closing the form, it closes the form so that when you open it again, everything    resets
        Me.Dispose()
    End Sub


    Private Sub frmTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Makes all the things invisible before the actual test starts
        lblDef.Visible = False
        lblWeekNo2.Visible = False
        lblInfo.Visible = False
        lblDefinition.Visible = False
        txtAns.Visible = False
        lblWeekNo.Visible = False
        btnNxt.Visible = False
        lblAnswer.Visible = False
        ' Opening the connection
        Conn = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=Database for VB.accdb")
        Conn.Open()
    End Sub


    Private Sub btnNxt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNxt.Click
        If r = 10 Then
            Ansrs(0, r - 1) = txtAns.Text
            txtAns.Text = ""
            FinishTest()
        Else
            Ansrs(1, r - 1) = txtAns.Text
            r = r + 1
            txtAns.Text = ""
            StartTest()
        End If
    End Sub
    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        ' Makes all the buttons that you will need in the test, visible. It'll show the field for the user to complete
        txtAns.Visible = True
        lblWeekNo.Visible = True
        lblWeekNo2.Visible = True
        lblDef.Visible = True
        lblDefinition.Visible = True
        btnStart.Visible = False
        lblAnswer.Visible = True
        ' When the button 'Start the test!' is clicked, the 'StartTest' sub routine will start
        StartTest()
    End Sub
    Private Sub StartTest()
        ' Making the 'Next' button visible to allow the user to complete the whole test
        btnNxt.Visible = True
        ' Reading data from the database
        Dim StringStart As String = "SELECT * FROM TestData WHERE WordNumber = " & r
        Dim Comm As New OleDb.OleDbCommand(StringStart, Conn)
        Dim RDR As OleDb.OleDbDataReader = Comm.ExecuteReader
        RDR.Read()
        lblDef.Text = RDR.Item("Definition")
        Ansrs(0, r - 1) = RDR.Item("Answer")
        lblWeekNo2.Text = RDR.Item("WeekNumber")
        lblInfo.Visible = True
        lblInfo.Text = r & "/10"
    End Sub
    Private Sub FinishTest()
        lblInfo.Text = "You have successfully completed the test! Well done!"
        lblInfo.Left = (Me.Width / 1.5) - (lblInfo.Width / 1.5)
        ' When the test is done, it makes all of the things that the user won't need anymore, invisible
        lblDef.Visible = False
        lblWeekNo2.Visible = False
        btnNxt.Visible = False
        lblWeekNo.Visible = False
        txtAns.Visible = False
        lblAnswer.Visible = False
        lblDefinition.Visible = False
        TestScore()
    End Sub
    Private Sub TestScore()
        For r As Integer = 0 To 9
            MsgBox(Ansrs(0, r))
            If Ansrs(0, r).ToLower = Ansrs(1, r).ToLower Then
                Score = Score + 2
                MsgBox("You have scored " & Score)
            End If
        Next
    End Sub
End Class


Thanks in advance and I hope that somebody will be able to help me!
 
Last edited:
Have you debugged your code? I'm guessing not. Just reading your code will only get you so far. If you could pick up all errors that way then you probably wouldn't have made those errors in the first place. When reading code, you tend to see what you expect and often miss what's actually there. Also, it's hard to know what data is going to be used by any particular bit of code. That's why you debug.

Place a breakpoint at the top of the code then step through the code line by line. You can the evaluate the state at each step to make sure that all variables, etc, evaluate to what you expect and execution takes the course that you expect. As soon as reality differs from expectation, you've found an issue. You can then work back to fix it.
 
Have you debugged your code? I'm guessing not. Just reading your code will only get you so far. If you could pick up all errors that way then you probably wouldn't have made those errors in the first place. When reading code, you tend to see what you expect and often miss what's actually there. Also, it's hard to know what data is going to be used by any particular bit of code. That's why you debug.

Place a breakpoint at the top of the code then step through the code line by line. You can the evaluate the state at each step to make sure that all variables, etc, evaluate to what you expect and execution takes the course that you expect. As soon as reality differs from expectation, you've found an issue. You can then work back to fix it.

I always debug my code. I'm actually a little confused now. I don't really get how that break point thing works. (Yes I know, I'm a noob. ;d) Would it be easier if I gave you my program? If so, what should I upload my program to, to make it easier for you?
Thanks.
 
I always debug my code. I'm actually a little confused now. I don't really get how that break point thing works. (Yes I know, I'm a noob. ;d) Would it be easier if I gave you my program? If so, what should I upload my program to, to make it easier for you?
Thanks.

If you don't know how a breakpoint works then you haven't really debugged your code. That's alright though; we all have to learn in the beginning.

Yes, it would be easier for you to hand over your code and let someone else fix it but that's not really what I'm here for. That might solve your current issue but learning value would be limited. If you were going to provide all you code then you would delete the 'obj' and 'bin' folders from each project, zip the solution folder and then attach the archive to a post. That should be a last resort though, and we're not there yet.

To set a breakpoint on a line of code, you can either click in the left-hand margin of the code window or click the line and then hit the F9 key. When you run your project, the debugger will then break when execution reaches that line. You can then use various tools, including the Autos, Locals, Watch and Immediate windows to evaluate variables, properties, methods and just about any other expression you like to determine the state of your app. You can then start the app running again (F5) or you can step through the code line by line (F8 or F10, depending on settings). You can also step into a method call using F11.

By stepping through the code and evaluating the state at each step, you can ensure that the state is as you expect all the way. If you don't know what the state should be then you had better think about it because, if you don't know what your app is supposed to do, what are the chances that you can make it do it? As I said before, as soon as the actual state differs from the expected state, you know that you've found an issue. You have a much better idea of what and where the issue is so finding a solution is much more likely. Even if you still can't solve the problem, at least you can give us a much better description and narrow down the parameters. By making it easier for us to help, you increase the likelihood that we will.
 
Back
Top