0

I'm trying to satisfy all the cases of multiple check boxes being checked at the same time. The first four 'if' statements are setting the values of each check box. Then I have started trying to do a Select-Case for multiple check boxes but it's not working. Just wondering if there is a more efficient way of doing this? Thanks.

        If chkCut.Checked = True Then
            serviceRate = 30.0
        End If

        If chkColour.Checked = True Then
            serviceRate = 40.0
        End If

        If chkHighlights.Checked = True Then
            serviceRate = 50.0
        End If

        If chkExtensions.Checked = True Then
            serviceRate = 200.0
        End If

        Dim i As Integer
        Select Case i
            Case chkCut.Checked And chkColour.Checked
                i = baseRate + 70.0
            Case chkCut.Checked And chkHighlights.Checked
                i = baseRate + 80.0
            Case chkCut.Checked And chkExtensions.Checked
                i = baseRate + 230.0

2 Answers 2

1

You want to have the sum of the selected values, therefore sum up the selected values.

serviceRate = 0.0
If chkCut.Checked Then
    serviceRate += 30.0
End If
If chkColour.Checked Then
    serviceRate += 40.0
End If
' Do the same for the other checkboxes
...

The expression serviceRate += 30.0 is a shorthand form of serviceRate = serviceRate + 30.0.

If you have 4 checkboxes you have 2 × 2 × 2 × 2 = 24 = 16 ways of checking or not checking them. A very tedious task with Select Case!


Btw: The Select...Case Statement selects one case according to a given value. This is not what you tried to do. You should have used an If...Then...Else Statement statement. However, this is obsolete now.

Sign up to request clarification or add additional context in comments.

5 Comments

Ok, Is it normal for the price to increase every time I click calculate or should I be coding something that avoids that?
Reset the price before starting a new calculation: serviceRate = 0.0
In your logic, you will need to reset the rate at some point to avoid this situation.
I guess setting serviceRate to 0 takes care of that problem. Yaa that's why I came on here to see if there was an easier way to do it because it sounded ridiculous.
My logic always initializes serviceRate to 0.0. So it can be called each time a checkbox is checked or unchecked. Instead you could also initialize serviceRate = baseRate and get the end result immediately.
0

The Linq geek in me would do something like this:

Subclass CheckBox and add a Rate property:

Public Class RateCheckBox
    Inherits CheckBox

    Private _Rate As Decimal
    Public Property Rate() As Decimal
        Get
            Return _Rate
        End Get
        Set(ByVal value As Decimal)
            _Rate = value
        End Set
    End Property

End Class

Then in the form, I'd have a button click handler and helper function like this:

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Rate As Decimal = CalcRate()
        Label1.Text = Rate.ToString()
    End Sub

    Private Function CalcRate() As Decimal
        Return Me.Controls.OfType(Of RateCheckBox).Where(Function(c) c.Checked).Sum(Function(c) c.Rate)
    End Function
End Class

Now, when you create your form, you can use your own RateCheckBox class instead of the regular CheckBox, and in the designer's property window, you can set the rate associated to each checkbox.

What I like about this approach is that it's scalable, meaning that no matter how many checkboxes you add to the form, it will always calculate the right amount without you having to change the calculation function.

Cheers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.