question

DucNguyen-0389 avatar image
0 Votes"
DucNguyen-0389 asked LesHay-2099 commented

Can I use a loop to shorten this?

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



dotnet-visual-basic
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi
Not much can be done there as far as I can see.

I could offer an alternative approach that overcomes some of the repeating of code blocks. Initially I thought that using Class would enable more concise coding, then I figured perhaps using a DataTable might be a better approach. In any case, if you want an example just post here and I (or someone else) will offer something.

Something like this:

200705-111.png


0 Votes 0 ·
111.png (26.2 KiB)

0 Answers