none
Problems with my calculation in Select Case RRS feed

  • Question

  • Hello All,

    I would like to thank all those who helped me last time. I restarted again and been trying to get help from my professor. Here is the code it works (YAY) but the calculation does not every single time I can't seem to get it to work. If anyone has any ideas why it won't be working or anything.

    '****************************************************************************
    'Program     : SalespersonsCommissions
    'Author      : Elisabeth van der Wilt
    'Student ID  : 3341248
    'Course ID   : CMIS 214
    'Assement    : Assignment 2 ~ Program 1
    'Date        : March 27th 2018
    '
    'Purpose     : To create a calculator for a salespersons commissions
    '              The application has two textboxes where they can enter
    '              their information. And three buttons Calculate,Clear,
    '              and Exit. There will be a message box to show if it was invalid.
    '********************************************************************************
    Public Class salesperson
        Option Strict On
        Option Explicit On
        Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
    
            'clears the textboxes
            TxtLevel.Text = ""
            TxtSold.Text = ""
            lblMessage.Text = ""
            TxtLevel.Focus()
        End Sub
    
        Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
            'this will end the application
            End
        End Sub
    
        Private Sub TxtLevel_TextChanged(sender As Object, e As EventArgs) Handles TxtLevel.TextChanged
    
            'checks if the present value is a number
            If Not IsNumeric(TxtLevel.Text) Then
                MessageBox.Show("The Present value is NOT numeric.")
                TxtLevel.Select() 'places cursor in that textbox
                Exit Sub 'breaks out of procedure
            End If
    
            'checks if the present value is a number between 1 and 4
            If Val(TxtLevel.Text) < 1 Or Val(TxtLevel.Text) > 4 Then
                MessageBox.Show("The Present value is not between 1 - 4")
                TxtLevel.Select() 'places cursor in that textbox.
                Exit Sub 'breaks out of procedure
            End If
        End Sub
    
        Private Sub TxtSold_TextChanged(sender As Object, e As EventArgs) Handles TxtSold.TextChanged
    
            'checks if the present value is a number
            If Not IsNumeric(TxtSold.Text) Then
                MessageBox.Show("The Present value is NOT numeric.")
                TxtSold.Select() 'places cursor in that textbox
                Exit Sub 'breaks out of procedure
            End If
    
            'checks if the present value is a number between 1 and 100,000
            If Val(TxtSold.Text) < 1 Or Val(TxtSold.Text) > 100000 Then
                MessageBox.Show("The Present value is not between $1 - $100,000")
                TxtSold.Select() 'places cursor in that textbox.
                Exit Sub 'breaks out of procedure
            End If
        End Sub
    
        Private Sub BtnCalculate_Click(sender As Object, e As EventArgs) Handles BtnCalculate.Click
    
            'this is to declare all the label and textboxs I have.
            Dim dblSold As Integer
            Dim dblLevel As Double
            Dim dblMessage As Double
    
            lblMessage.Text = Format(dblMessage, "C") 'this formats the textbox to currency
    
            Select Case dblLevel 'case determined by txtLevel
    
                Case "1" 'determined by sales level
                    dblLevel = dblSold * 0.02 + 500
                Case "2" 'determined by sales level
                    dblLevel = dblSold * 0.03 + 750
                Case "3" 'determined by sales level
                    dblLevel = dblSold * 0.04 + 1000
                Case "4" 'determined by sales level
                    dblLevel = dblSold * 0.05 + 1250
                Case Else
    
                    Exit Select
            End Select
    
        End Sub
    
    End Class

    I would appreciate all help that is possible.

    Thursday, April 5, 2018 5:39 PM

All replies

  •     Dim dblLevel As Double
            Dim dblMessage As Double
    
            lblMessage.Text = Format(dblMessage, "C") 'this formats the textbox to currency
    
            Select Case dblLevel 'case determined by txtLevel
    

    dbLevel is always empty so the select simply exit. 

    In your code is a statement "End" to close the program, use "Close". "End" kills a program without to entomb


    Success
    Cor

    Thursday, April 5, 2018 5:53 PM
  • You say this is why

     'this will end the application
            End

    <g class="gr_ gr_18 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" data-gr-id="18" id="18">its</g> not working?

    So what is the code for close do I just write close instead of <g class="gr_ gr_69 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" data-gr-id="69" id="69">end</g>?

    I am sorry <g class="gr_ gr_93 gr-alert gr_tiny gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" data-gr-id="93" id="93">i</g> am terrible at this coding.

    Thursday, April 5, 2018 6:00 PM
  • Hi

    In your Select block, you are using 'dblLevel' as the switch. That variable is declared as a Double type, but in the Case...End Case block, you test it as a String (eg  Case "1") - it can't be both.

    If you hadn't removed the Option Strict On, then some of this would have been flagged for you.

    Also,in the code you posted,I can't seem to find where dblLevel is assigned any value at all.


    Regards Les, Livingston, Scotland

    • Proposed as answer by Ashidacchi Thursday, April 5, 2018 11:41 PM
    Thursday, April 5, 2018 6:02 PM
  • So how do I make it gather the information to be case 1 and gather the info from dblLevel as thats the one textbox? 

    Assigned any value? What does this mean? 
    Thursday, April 5, 2018 6:07 PM
  • I changed it to Me.Close() instead of End. Will that help Cor? I tried it but my calculation still did not add and multiple I also removed option strict on and explicit on. and removed that
    Thursday, April 5, 2018 6:21 PM
  • Maybe also replace

       Select Case dblLevel

    with

       Select Case TxtLevel.Text

     

    After End Select, do something with the result — dblLevel. For example, show it in the label.


    • Edited by Viorel_MVP Thursday, April 5, 2018 6:35 PM
    Thursday, April 5, 2018 6:32 PM
  • I also removed option strict on and explicit on. and removed that

    Hi

    You are just hiding things that will bite you later on by doing that.


    Regards Les, Livingston, Scotland

    Thursday, April 5, 2018 6:37 PM
  • I changed it to txtlevel.text for some reason it doesn't want to do my calculation it pops up (YAY) but no numbers.
    Thursday, April 5, 2018 6:39 PM
  • Seems that dblSold is nothing too. Try this intermediate adjustment:

       dblSold = Val(TxtSold.Text)

       Select Case TxtLevel.Text

          . . .

       End Select

       lblMessage.Text = Format(dblLevel, "C")

    Thursday, April 5, 2018 6:50 PM
  • I did as Viorel_ did I will post all code again and let you know. I am appreciating and very grateful for all the help. 

    '****************************************************************************
    'Program     : SalespersonsCommissions
    'Author      : Elisabeth van der Wilt
    'Student ID  : 3341248
    'Course ID   : CMIS 214
    'Assement    : Assignment 2 ~ Program 1
    'Date        : March 27th 2018
    '
    'Purpose     : To create a calculator for a salespersons commissions
    '              The application has two textboxes where they can enter
    '              their information. And three buttons Calculate,Clear,
    '              and Exit. There will be a message box to show if it was invalid.
    '
    'Date Revision: April 5th 2018
    'Revisions   : Got some help from my post goo.gl/1V1wrm . Was able to turn integer 
    '              to double. 
    '********************************************************************************
    Option Strict On
    Option Explicit On
    Public Class salesperson
        Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
    
            'clears the textboxes
            TxtLevel.Text = ""
            TxtSold.Text = ""
            lblMessage.Text = ""
            TxtLevel.Focus()
        End Sub
    
        Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
            'this will end the application
            Me.Close()
    
        End Sub
    
        Private Sub TxtLevel_TextChanged(sender As Object, e As EventArgs) Handles TxtLevel.TextChanged
    
            'checks if the present value is a number
            If Not IsNumeric(TxtLevel.Text) Then
                MessageBox.Show("The Present value is NOT numeric.")
                TxtLevel.Select() 'places cursor in that textbox
                Exit Sub 'breaks out of procedure
            End If
    
            'checks if the present value is a number between 1 and 4
            If Val(TxtLevel.Text) < 1 Or Val(TxtLevel.Text) > 4 Then
                MessageBox.Show("The Present value is not between 1 - 4")
                TxtLevel.Select() 'places cursor in that textbox.
                Exit Sub 'breaks out of procedure
            End If
        End Sub
    
        Private Sub TxtSold_TextChanged(sender As Object, e As EventArgs) Handles TxtSold.TextChanged
    
            'checks if the present value is a number
            If Not IsNumeric(TxtSold.Text) Then
                MessageBox.Show("The Present value is NOT numeric.")
                TxtSold.Select() 'places cursor in that textbox
                Exit Sub 'breaks out of procedure
            End If
    
            'checks if the present value is a number between 1 and 100,000
            If Val(TxtSold.Text) < 1 Or Val(TxtSold.Text) > 100000 Then
                MessageBox.Show("The Present value is not between $1 - $100,000")
                TxtSold.Select() 'places cursor in that textbox.
                Exit Sub 'breaks out of procedure
            End If
        End Sub
    
        Private Sub BtnCalculate_Click(sender As Object, e As EventArgs) Handles BtnCalculate.Click
    
            'this is to declare all the label and textboxs I have.
            Dim dblSold As Integer
            Dim dblLevel As Double
            Dim dblMessage As Double
    
    
            dblSold = Val(TxtSold.Text)
    
            Select Case TxtLevel.Text 'case determined by txtLevel
    
                Case "1" 'determined by sales level
                    dblLevel = dblSold * 0.02 + 500
                Case "2" 'determined by sales level
                    dblLevel = dblSold * 0.03 + 750
                Case "3" 'determined by sales level
                    dblLevel = dblSold * 0.04 + 1000
                Case "4" 'determined by sales level
                    dblLevel = dblSold * 0.05 + 1250
                Case Else
    
                    Exit Select
            End Select
    
            lblMessage.Text = Format(dblMessage, "C") 'this formats the textbox to currency
    
        End Sub
    
    End Class

    With Option Strict and Explicit back on its informing me that it is wrong. BC30512  I am getting this error.  Option Strict On disallows implicit conversions from 'Double' to 'Integer'.

    Edit: Even after adding their fix of CInt in front it doesn't work. Is my calculation wrong?
    • Edited by Lisa213 Thursday, April 5, 2018 7:11 PM
    Thursday, April 5, 2018 7:09 PM
  • Hi

    That code is very much better than before. The Options settings you are now using are doing their job showing that error.

    Which line was the error detected on? Where did you add CInt?


    Regards Les, Livingston, Scotland

    Thursday, April 5, 2018 7:32 PM
  • dblSold = Val(TxtSold.Text) 

    this one does

    I added it but it does not work. When added or is not added. 

    • Edited by Lisa213 Thursday, April 5, 2018 7:34 PM
    Thursday, April 5, 2018 7:34 PM
  • dblSold = Val(TxtSold.Text) 
    this one does

    Hi

    ........and, what is the TxtSold.Text string?


    Regards Les, Livingston, Scotland

    Thursday, April 5, 2018 7:36 PM
  • Hi

    Here is a way to help avoid some of the issues.

    This is a Function, which you pass any string (say a TextBox.Text) and it will return a valid number every time. The value returned will be an Integer accurate to the Text passed to it. If the String passed to it can not be converted to an Integer, then it will return zero (a valid Integer). There are advantages in doing it this way, as the results of using any String value in a numeric calculation will not fail (other than a division by zero which would be an issue in any case), and, it is reusable for any needed convertions throughout yourcode.

    NOTE: there are similar Functions that can be used for other types (eg GetDouble)

    This Function is used like this

    dblSold = GetInteger(TxtSold.Text) * 0.4

    Function GetInteger(s As String) As Integer
       Dim v As Integer = 0
       If Integer.TryParse(s, v) Then Return v
       Return 0
    End Function


    Regards Les, Livingston, Scotland

    • Proposed as answer by Stanly Fan Monday, April 9, 2018 6:22 AM
    Thursday, April 5, 2018 7:47 PM
  • With Option Strict and Explicit back on its informing me that it is wrong. BC30512  I am getting this error.  Option Strict On disallows implicit conversions from 'Double' to 'Integer'.

    Edit: Even after adding their fix of CInt in front it doesn't work. Is my calculation wrong?

    Using option strict on will enforce to use proper coding methods.

    Dim x As Integer = 10

    Dim y As Double = 20.25

    Dim z As Decimal = 10.01201

    Dim s As String = "10.12"

    All of the above can be converted to another type. The string can be converted to Decimal and Double types but if converted to Integer it will lose the decimal value of .12 and be converted to 10.

    The x, y and z can all be converted to a string type. The y and z can be converted to the others type. The x can be widened to y and z and converted to s types. However converting y, z or s to x type will lose the decimal values as an integer can not contain decimal values.

    Also see Implicit and Explicit Conversions (Visual Basic) and Conversions Between Strings and Other Types (Visual Basic).


    La vida loca

    • Edited by Mr. Monkeyboy Thursday, April 5, 2018 8:32 PM
    • Proposed as answer by Stanly Fan Monday, April 9, 2018 6:22 AM
    Thursday, April 5, 2018 8:30 PM