Solved

There must be a more efficient way to varify if the correct radio button has been selected.

Posted on 2008-10-11
6
137 Views
Last Modified: 2010-04-23
There must be a more efficient way of verifying if the correct radio button has been selected. Any suggestions?
Private Sub btbOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btbOK.Click
 

        Dim BoardForm As New frmQuestion
 

        If RB1.Tag = m_CorrectAnswer Then

            PlayerTotal = (m_sender * 200) + PlayerTotal

            MessageBox.Show("Correct!", "Correct", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        Else

            MessageBox.Show("Sorry", "Wrong", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        End If

        If RB2.Tag = m_CorrectAnswer Then

            PlayerTotal = (m_sender * 200) + PlayerTotal

            MessageBox.Show("That is Correct!", "Correct", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        Else

            MessageBox.Show("That is wrong!", "Wrong", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        End If

        If RB3.Tag = m_CorrectAnswer Then

            PlayerTotal = (m_sender * 200) + PlayerTotal

            MessageBox.Show("That is Correct!", "Correct", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        Else

            MessageBox.Show("That is wrong!", "Wrong", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        End If

        If RB4.Tag = m_CorrectAnswer Then

            PlayerTotal = (m_sender * 200) + PlayerTotal

            MessageBox.Show("That is Correct!", "Correct", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        Else

            MessageBox.Show("That is wrong!", "Wrong", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        End If

        Me.Close()

    End Sub

Open in new window

0
Comment
Question by:98fatboyrider
6 Comments
 
LVL 42

Expert Comment

by:dqmq
ID: 22694379
First, below I show some (trivially) more efficient code.  Perhaps more importantly, it's more concise and to the point.

Second, you may want to consider putting the radio buttons in an option group.  The benefits are that you can reduce the OK button to a single test:
   If OptionGroup = m_correctAnswer
         Msgbox "correct"
   Else
         Msgbox "wrong"
   end if

Also, when one is selected the others will automatically go unselected so you don't have to manage that in code.


   If RB1.Tag = m_CorrectAnswer

or RB2.Tag = m_CorrectAnswer 

or RB3.Tag = m_CorrectAnswer 

or RB4.Tag = m_CorrectAnswer 

or RB5.Tag = m_CorrectAnswer  

Then

            PlayerTotal = (m_sender * 200) + PlayerTotal

            MessageBox.Show("Correct!", "Correct", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        Else

            MessageBox.Show("Sorry", "Wrong", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        End If

Open in new window

0
 

Author Comment

by:98fatboyrider
ID: 22694423
When using the code above, I'm getting syntax errors on all the "or" and "then". This is vb.net if that helps.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 22694507
Well...you can use the UnderScore "_" character as a line continuation flag, or put it all on the same line.

I would also use "OrElse" instead of "Or" so that it short-circuits at the first match and doesn't need to check the rest of the RadioButtons.

So like this:

    If RB1.Tag = m_CorrectAnswer _
    OrElse RB2.Tag = m_CorrectAnswer _
    OrElse RB3.Tag = m_CorrectAnswer _
    OrElse RB4.Tag = m_CorrectAnswer _
    OrElse RB5.Tag = m_CorrectAnswer _
    Then

Or like this:
    Private Sub btbOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btbOK.Click

        If RB1.Tag = m_CorrectAnswer OrElse RB2.Tag = m_CorrectAnswer OrElse RB3.Tag = m_CorrectAnswer OrElse RB4.Tag = m_CorrectAnswer OrElse RB5.Tag = m_CorrectAnswer Then

            PlayerTotal = (m_sender * 200) + PlayerTotal

            MessageBox.Show("Correct!", "Correct", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        Else

            MessageBox.Show("Sorry", "Wrong", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        End If

        Me.Close()

    End Sub

Open in new window

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22694553
You can use a custom control like this:
http://www.codeproject.com/KB/combobox/RadioListBoxDotNetVersion.aspx
It simultates a radio button collection and you can retrieve in one step with SelectedIndex
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22694575
Pretty cool control jaime...  =)

Another generic option is to iterate over the range of names you want to check and find the control on the form.

I wouldn't say this is more "efficient" than hard coding if the radiobuttons are static but I thought I would present it anyways since it has many uses:
(this version is for VB.Net 2005 or above)
    Private Sub btbOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btbOK.Click

        Dim correct As Boolean = False

        For i As Integer = 1 To 5

            Dim matches() As Control = Me.Controls.Find("RB" & i, True)

            If matches.Length > 0 AndAlso TypeOf matches(0) Is RadioButton Then

                If matches(0).Tag = m_CorrectAnswer Then

                    correct = True

                    Exit For

                End If

            End If

        Next
 

        If correct Then

            PlayerTotal = (m_sender * 200) + PlayerTotal

            MessageBox.Show("Correct!", "Correct", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        Else

            MessageBox.Show("Sorry", "Wrong", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

        End If

        Me.Close()

    End Sub

Open in new window

0
 

Author Closing Comment

by:98fatboyrider
ID: 31505319
Thanks for the help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The viewer will learn how to implement Singleton Design Pattern in Java.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now