I'm wondering if there's a way to cut the long statement and make it shorter as possible.
the case is i have 5 radiobuttons each questions.

thanks in advance.. =)

If Q1.Visible = True Then
            If q111.Checked = True Or q112.Checked = True Or q113.Checked = True Or q114.Checked = True Or q115.Checked = True Then
                If q111.Checked = True Then
                    sm = sm + 5
                ElseIf q112.Checked = True Then
                    sm = sm + 4.5
                ElseIf q113.Checked = True Then
                    sm = sm + 4
                ElseIf q114.Checked = True Then
                    sm = sm + 3.5
                Else
                    sm = sm + 3
                End If
                If q121.Checked = True Or q122.Checked = True Or q123.Checked = True Or q124.Checked = True Or q125.Checked = True Then
                    If q121.Checked = True Then
                        sm = sm + 5
                    ElseIf q122.Checked = True Then
                        sm = sm + 4.5
                    ElseIf q123.Checked = True Then
                        sm = sm + 4
                    ElseIf q124.Checked = True Then
                        sm = sm + 3.5
                    Else
                        sm = sm + 3
                    End If
                    If q131.Checked = True Or q132.Checked = True Or q133.Checked = True Or q134.Checked = True Or q135.Checked = True Then
                        If q131.Checked = True Then
                            sm = sm + 5
                        ElseIf q132.Checked = True Then
                            sm = sm + 4.5
                        ElseIf q133.Checked = True Then
                            sm = sm + 4
                        ElseIf q134.Checked = True Then
                            sm = sm + 3.5
                        Else
                            sm = sm + 3
                        End If
                        If q141.Checked = True Or q142.Checked = True Or q143.Checked = True Or q144.Checked = True Or q145.Checked = True Then
                            If q141.Checked = True Then
                                sm = sm + 5
                            ElseIf q142.Checked = True Then
                                sm = sm + 4.5
                            ElseIf q143.Checked = True Then
                                sm = sm + 4
                            ElseIf q144.Checked = True Then
                                sm = sm + 3.5
                            Else
                                sm = sm + 3
                            End If
                            If q151.Checked = True Or q152.Checked = True Or q153.Checked = True Or q154.Checked = True Or q155.Checked = True Then
                                If q151.Checked = True Then
                                    sm = sm + 5
                                ElseIf q152.Checked = True Then
                                    sm = sm + 4.5
                                ElseIf q153.Checked = True Then
                                    sm = sm + 4
                                ElseIf q154.Checked = True Then
                                    sm = sm + 3.5
                                Else
                                    sm = sm + 3
                                End If
                                MsgBox(Math.Round(sm))
                                Q1.Visible = False
                                Q2.Visible = True
                            End If
                        End If
                    End If
                End If
            End If

Recommended Answers

All 9 Replies

        If Q1.Visible = True Then
            Dim numb As Integer = 5
            For i = 111 To 155
                Dim q As RadioButton = CType(Me.Controls("q" & i), RadioButton)
                If i.ToString.EndsWith("1") Then
                    numb = 5
                End If
                If q.Created = True Then
                    If q.Checked = True Then
                        sm = sm + numb
                        numb = numb - 0.5
                    Else
                        sm = sm + 3
                    End If
                End If
            Next
            MsgBox(Math.Round(sm))
            Q2.Visible = True
        End If

@oussama - How long did it take you to unravel that?

For expressions that already evaluate to Boolean there is no need to do the comparison so

If q.Created = True Then

can be written as

If q.Created Then
commented: got it, thanks +3

still doesnt work sir

Perhaps if you explained the logic behind the code we could suggest a shorter alternative.

there are 5 questions and each question have 5 radiobuttons as q111 to q155
each question you can only choose 1 radiobutton obviously. and the value of each button is different with each other.

for example all 5 questions checked the radiobuttons q111, q121, q131, q141 and q151 the variable sm should return 25

That's not "explaining the logic". That's just giving one example of what happens when you select one particular set of buttons. Are the radio buttons for each question worth 1, 2, 3, 4 and 5 points respectively?

oh sorry
i mean for all radiobuttons
named that starts with q1 and end with 1 the value is 5,
for the name that starts with q1 and end with 2 the value is 4,
for the name that starts with q1 and end with 3 the value is 3,
for the name that starts with q1 and end with 4 the value is 2,
for the name that starts with q1 and end with 5 the value is 1

You are giving snippets of information instead of describing the problem. If you are not going to post the problem you are trying to solve with enough information for us to provide some help then we are not going to go to the trouble of dragging it out of you by playing 20 questions. You say the values are 1, 2, 3, 4 and 5, but your code has values like 3.5 and 4.5. What you say and the code you post do not agree.

I will make a couple of suggestions that may help. You can access a control by creating the name dynamically. For example, if you have five radiobuttons named q111, q112, q113, q114 and q115, you can access each in turn by

For i As Integer = 1 To 5
    Dim rad As RadioButton = Me.Controls("q11" & i)
    If rad.Checked
        .
        .
        .
    End If
Next

Keep in mind that RadioButtons act as a group. If you want to treat groups of five buttons as separate groups then you should put them in a container like a Panel, or perhaps better in your case, a GroupBox. In that case the above code could be modified to

For g As Integer = 1 To 5
    For i As Integer = 1 To 5
        Dim rad As RadioButton = Me.Controls("g" & g & "1" & i)
        If rad.Checked
            .
            .
            .
        End If
    Next
Next
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.