Can I use a loop to shorten this?

Duc Nguyen 101 Reputation points
2022-05-10T05:17:44.477+00:00

I have to face this problem where I will have to do 15 cases like below (only 2 cases below). I am wondering if I can use a loop or some way shorten the code. Will it be a good idea to use a loop or should I keep it as it is (which is 15 cases instead) ?

  Private Sub TVInEachRoom(sender As Object, e As EventArgs) Handles chkBed1TV.CheckedChanged, chkBed2TV.CheckedChanged

        Dim che As CheckBox = DirectCast(sender, CheckBox)  'Cast the sender to a Label

        Select Case che.Name


            Case "chkBed1TV"
                'Check if user select tv in bed1
                If chkBed1TV.Checked = True Then
                    Options(3, 1) = Options(3, 1) + BEDROOMTV 'Store (adding) bed room tv price into an element of the array
                End If
                'check to see if user cancel after selected tv
                If chkBed1TV.Checked = False Then
                    Options(3, 1) = Options(3, 1) - BEDROOMTV 'Store (Misnusing) bed room tv into an element of the array
                End If

            Case "chkBed2TV"
                'Check if user select tv in bed2
                If chkBed2TV.Checked = True Then
                    Options(4, 1) = Options(4, 1) + BEDROOMTV 'Store (adding) bed room tv price into an element of the array
                End If
                'check to see if user cancel after selected tv
                If chkBed2TV.Checked = False Then
                    Options(4, 1) = Options(4, 1) - BEDROOMTV 'Store (Misnusing) bed room tv into an element of the array
                End If

        End Select
End Sub
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,658 questions
{count} votes