#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    5
    Rep Power
    0

    Learning VB - How to reduce code length


    Hi,
    just starting to learn and am looking for suggestions on how to make this code more compact. whether it is with the use of classes, functions or different logical programming. This is one of my first programming tries. i have no need to reduce code, only want to learn more about different ways of doing things in VB.net. Thanks

    PHP Code:
    Public Class frmLittles
        Dim intSolveFor 
    As Integer

        
    Private Sub rdoInventory_CheckedChanged(sender As ObjectAs EventArgsHandles rdoInventory.CheckedChanged
            intSolveFor 
    'value if checkbox1 is checked.
            txtRate.Enabled = True '
    when checked enable these 2 text boxes
            txtTime
    .Enabled True
            txtInventory
    .Enabled False 'disable this textbox
            lblAnswer.Text = "" '
    clear answer label
        End Sub

        
    Private Sub frmLittles_Load(sender As ObjectAs EventArgsHandles MyBase.Load



        End Sub

        
    Private Sub rdoRate_CheckedChanged(sender As ObjectAs EventArgsHandles rdoRate.CheckedChanged
            intSolveFor 
    'value if checkbox 2 is checked
            txtInventory.Enabled = True '
    enable these 2 text boxes
            txtTime
    .Enabled True
            txtRate
    .Enabled False 'disable this textbox
            lblAnswer.Text = "" '
    clear answer label
        End Sub

        
    Private Sub rdoTime_CheckedChanged(sender As ObjectAs EventArgsHandles rdoTime.CheckedChanged
            intSolveFor 
    'value if checkbox3 is checked
            txtInventory.Enabled = True '
    enable these 2 textboxes
            txtRate
    .Enabled True
            txtTime
    .Enabled False 'disable this textbox
            lblAnswer.Text = "" '
    clear answer label
        End Sub

        
    Private Sub btnSolve_Click(sender As ObjectAs EventArgsHandles btnSolve.Click
            Dim intInventory 
    As Integer
            Dim intRate 
    As Integer
            Dim intTime 
    As Integer

            Select 
    Case intSolveFor
                
    Case 1
                    intRate 
    Convert.ToInt32(txtRate.Text'convert the 2 textbox strings to integers
                    intTime = Convert.ToInt32(txtTime.Text)

                    If intTime = 0 Or intRate = 0 Then '
    test for or no answer in enabled textboxes
                        MsgBox
    ("Cannot enter 0 as Rate or Time.  Please correct, thank you."'message if true
                    Else
                        lblAnswer.Text = intRate * intTime '
    compute
                    End 
    If
                    
    txtRate.Text "" 'clear the 2 textboxes
                    txtTime.Text = ""

                Case 2
                    intInventory = Convert.ToInt32(txtInventory.Text)
                    intTime = Convert.ToInt32(txtTime.Text)

                    If intInventory = 0 Or intTime = 0 Then
                        MsgBox("Cannot enter 0 as Inventory or Time.  Please correct, thank you.")
                    Else
                        lblAnswer.Text = intTime / intInventory
                    End If
                    txtInventory.Text = ""
                    txtTime.Text = ""

                Case 3
                    intInventory = Convert.ToInt32(txtInventory.Text)
                    intRate = Convert.ToInt32(txtRate.Text)

                    If intInventory = 0 Or intRate = 0 Then
                        MsgBox("Cannot enter 0 as Inventory or Rate.  Please correct, thank you.")
                    Else
                        lblAnswer.Text = intRate / intInventory
                    End If
                    txtInventory.Text = ""
                    txtRate.Text = ""
            End Select
        End Sub
    End Class 
    Thanks in advance for any suggestions
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,692
    Rep Power
    1958
    Don't think you can make it much more compact.
    You can have 3 lines less, if you read all the fields before the case-block. But it is not that effective to "optimize" for that kind of (small) changes.

    One thing I suggest you change, is the part where you do a reset/clear of the fields. Now it also clear when the fields have wrong input. I would place that part of code inside the else part.

    I can see you use the PHP code tags. For VB it is better to use the highlight tag which does support the VB syntax.
    For smalle code lines, there is also the code tag, which doesn't have syntax highlightning, but still keep the spaces.

    Code when using [highlight =vb] (without space)
    vb Code:
    Public Class frmLittles
        Dim intSolveFor As Integer
     
        Private Sub rdoInventory_CheckedChanged(sender As Object, e As EventArgs) Handles rdoInventory.CheckedChanged
            intSolveFor = 1 'value if checkbox1 is checked.
            txtRate.Enabled = True 'when checked enable these 2 text boxes
            txtTime.Enabled = True
            txtInventory.Enabled = False 'disable this textbox
            lblAnswer.Text = "" 'clear answer label
        End Sub
     
        Private Sub frmLittles_Load(sender As Object, e As EventArgs) Handles MyBase.Load
     
     
     
        End Sub
     
        Private Sub rdoRate_CheckedChanged(sender As Object, e As EventArgs) Handles rdoRate.CheckedChanged
            intSolveFor = 2 'value if checkbox 2 is checked
            txtInventory.Enabled = True 'enable these 2 text boxes
            txtTime.Enabled = True
            txtRate.Enabled = False 'disable this textbox
            lblAnswer.Text = "" 'clear answer label
        End Sub
     
        Private Sub rdoTime_CheckedChanged(sender As Object, e As EventArgs) Handles rdoTime.CheckedChanged
            intSolveFor = 3 'value if checkbox3 is checked
            txtInventory.Enabled = True 'enable these 2 textboxes
            txtRate.Enabled = True
            txtTime.Enabled = False 'disable this textbox
            lblAnswer.Text = "" 'clear answer label
        End Sub
     
        Private Sub btnSolve_Click(sender As Object, e As EventArgs) Handles btnSolve.Click
            Dim intInventory As Integer
            Dim intRate As Integer
            Dim intTime As Integer
     
            Select Case intSolveFor
                Case 1
                    intRate = Convert.ToInt32(txtRate.Text) 'convert the 2 textbox strings to integers
                    intTime = Convert.ToInt32(txtTime.Text)
     
                    If intTime = 0 Or intRate = 0 Then 'test for 0 or no answer in enabled textboxes
                        MsgBox("Cannot enter 0 as Rate or Time.  Please correct, thank you.") 'message if true
                    Else
                        lblAnswer.Text = intRate * intTime 'compute
                    End If
                    txtRate.Text = "" 'clear the 2 textboxes
                    txtTime.Text = ""
     
                Case 2
                    intInventory = Convert.ToInt32(txtInventory.Text)
                    intTime = Convert.ToInt32(txtTime.Text)
     
                    If intInventory = 0 Or intTime = 0 Then
                        MsgBox("Cannot enter 0 as Inventory or Time.  Please correct, thank you.")
                    Else
                        lblAnswer.Text = intTime / intInventory
                    End If
                    txtInventory.Text = ""
                    txtTime.Text = ""
     
                Case 3
                    intInventory = Convert.ToInt32(txtInventory.Text)
                    intRate = Convert.ToInt32(txtRate.Text)
     
                    If intInventory = 0 Or intRate = 0 Then
                        MsgBox("Cannot enter 0 as Inventory or Rate.  Please correct, thank you.")
                    Else
                        lblAnswer.Text = intRate / intInventory
                    End If
                    txtInventory.Text = ""
                    txtRate.Text = ""
            End Select
        End Sub
    End Class

IMN logo majestic logo threadwatch logo seochat tools logo