Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,075 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,787 people online right now. Registration is fast and FREE... Join Now!




Making a quiz using case statements

 
Reply to this topicStart new topic

Making a quiz using case statements

Semple
4 Mar, 2008 - 06:19 AM
Post #1

New D.I.C Head
*

Joined: 13 Feb, 2008
Posts: 12


My Contributions
Basically i've got five questions, with a choice of 3 answers to each question(radio buttons). I've decided to use a case function, to check if the correct radio button is selected. If it has then "1" will be added to the score. At the end of the questions the user clicks 'check', which runs the case function and then the score is output in the message box.


CODE
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim score As Integer = 0
        Dim quiz As String

        Select Case quiz
            Case 1 : If Me.RadTwo.Checked = True Then score = (+1)
            Case 2 : If Me.RadFive.Checked = True Then score = (+1)
            Case 3 : If Me.RadSeven.Checked = True Then score = (+1)
            Case 4 : If Me.RadTwelve.Checked = True Then score = (+1)
            Case 5 : If Me.RadFourteen.Checked = True Then score = (+1)
        End Select

        Call MsgBox("Congratulations, your score is " & score, vbOKOnly Or vbInformation, "Score")
    End Sub


Any help please, much appreciated.

User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Making A Quiz Using Case Statements
4 Mar, 2008 - 06:25 AM
Post #2

 101010101
Group Icon

Joined: 3 Aug, 2006
Posts: 1,812



Thanked: 1 times
Dream Kudos: 755
My Contributions
Can you please specify what errors/problems you have with your code?

I see one problem. The variable "quiz" is a string. In you Select-Case construct, you've treated it as an integer. I think, if quiz refers to the serial number of the question, you would do better declaring it as an integer.
User is offlineProfile CardPM
+Quote Post

Semple
RE: Making A Quiz Using Case Statements
4 Mar, 2008 - 06:36 AM
Post #3

New D.I.C Head
*

Joined: 13 Feb, 2008
Posts: 12


My Contributions
No matter if i select the right or wrong answers, the message box still tells me ive scored zero.

Ive swapped string with integer for the quiz variable, and still does the same thing.
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Making A Quiz Using Case Statements
4 Mar, 2008 - 06:53 AM
Post #4

 101010101
Group Icon

Joined: 3 Aug, 2006
Posts: 1,812



Thanked: 1 times
Dream Kudos: 755
My Contributions
Try,
CODE

Static Dim score As Integer = 0

Without static, score would get initialized to zero every time you clicked the button.

Also,
CODE

score+=1

The statement you have used, assigns 1 to score every time - it doesn't increase its value (I think that's what you want).

Hope this helps smile.gif.
User is offlineProfile CardPM
+Quote Post

Semple
RE: Making A Quiz Using Case Statements
4 Mar, 2008 - 06:58 AM
Post #5

New D.I.C Head
*

Joined: 13 Feb, 2008
Posts: 12


My Contributions
CODE
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Static Dim score As Integer = 0
        Dim quiz As Integer

        Select Case quiz
            Case 1 : If Me.RadTwo.Checked = True Then score += 1
            Case 2 : If Me.RadFive.Checked = True Then score += 1
            Case 3 : If Me.RadSeven.Checked = True Then score += 1
            Case 4 : If Me.RadTwelve.Checked = True Then score += 1
            Case 5 : If Me.RadFourteen.Checked = True Then score += 1
        End Select

        Call MsgBox("Congratulations, your score is " & score, vbOKOnly Or vbInformation, "Score")
            

    End Sub


sad.gif still counts as zero, does anyone see a problem with the way that it counts the scores, i have a feeling that its not counting properly.

This post has been edited by Semple: 4 Mar, 2008 - 06:59 AM
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Making A Quiz Using Case Statements
4 Mar, 2008 - 07:03 AM
Post #6

 101010101
Group Icon

Joined: 3 Aug, 2006
Posts: 1,812



Thanked: 1 times
Dream Kudos: 755
My Contributions
Works perfectly for me. Make sure you have selected a radio button - and the right one.
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Making A Quiz Using Case Statements
4 Mar, 2008 - 07:16 AM
Post #7

 101010101
Group Icon

Joined: 3 Aug, 2006
Posts: 1,812



Thanked: 1 times
Dream Kudos: 755
My Contributions
Also make sure that quiz has been assigned one of the values.
User is offlineProfile CardPM
+Quote Post

Semple
RE: Making A Quiz Using Case Statements
5 Mar, 2008 - 08:08 AM
Post #8

New D.I.C Head
*

Joined: 13 Feb, 2008
Posts: 12


My Contributions
What do you mean by "make sure the quiz has been assigned one of the values".
User is offlineProfile CardPM
+Quote Post

Sothrie
RE: Making A Quiz Using Case Statements
5 Mar, 2008 - 12:34 PM
Post #9

New D.I.C Head
*

Joined: 3 Dec, 2007
Posts: 25


My Contributions
Ok here's what I'm seeing:
In your button_click event you're dimming a variable 'quiz' which isn't set to anything so it defaults to zero.
Then when you are in your Case Select statement, which is based off of quiz, it's looking for the value of quiz. And the way it is written, the case it is looking for is Case 0.

So somewhere in the event, before the Select Case, you have to specify which quiz you are trying to score by passing a value to the quiz integer.

Hope that helps!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 11:09PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month