3 Replies - 3102 Views - Last Post: 26 January 2012 - 05:12 PM Rate Topic: -----

#1 Borron555  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 02-March 11

Using Integer.TryParse for validation an an If statement

Posted 26 January 2012 - 03:34 PM

hello, this is for homework but I have 90% of the project done, I just need to finish the data validation portion of the code.

This is what I have for the calculate click event:

 Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

        Dim intInputA As Integer
        Dim intInputB As Integer
        Dim intInputC As Integer

        Dim intPackageACost As Integer = 99
        Dim intPackageBCost As Integer = 199
        Dim intPackageCCost As Integer = 299

        Dim decPackageATotal As Decimal
        Dim decPackageBTotal As Decimal
        Dim decPackageCTotal As Decimal
        Dim decGrandTotal As Decimal

        Dim decDiscount As Decimal

        If Integer.TryParse(txtPackageA.Text, intInputA) Then
            intInputA = txtPackageA.Text

            If intInputA <= 0 Then
                lblTotalOut.Text = "You must enter a positive number for all units sold."
            End If
        Else
            lblTotalOut.Text = "Please enter a numeric Package A value"

        End If

        If Integer.TryParse(txtPackageB.Text, intInputB) Then
            intInputB = txtPackageB.Text

            If intInputC <= 0 Then
                lblTotalOut.Text = "You must enter a positive number for all units sold."
            End If
        Else
            lblTotalOut.Text = "Please enter a numeric Package B value"
        End If

        If Integer.TryParse(txtPackageC.Text, intInputC) Then
            intInputC = txtPackageC.Text
            If intInputC <= 0 Then
                lblTotalOut.Text = "You must enter a positive number for all units sold."
            End If
        Else
            lblTotalOut.Text = "Please enter a numeric Package C value"
        End If

        Select Case intInputA
            Case 10 To 19
                decDiscount = 0.2D
                decPackageATotal = (intInputA * intPackageACost) - (intInputA * intPackageACost * decDiscount)
            Case 20 To 49
                decDiscount = 0.3D
                decPackageATotal = (intInputA * intPackageACost) - (intInputA * intPackageACost * decDiscount)
            Case 50 To 99
                decDiscount = 0.4D
                decPackageATotal = (intInputA * intPackageACost) - (intInputA * intPackageACost * decDiscount)
            Case Is > 100
                decDiscount = 0.5D
                decPackageATotal = (intInputA * intPackageACost) - (intInputA * intPackageACost * decDiscount)
            Case Else
                decPackageATotal = intInputA * intPackageACost
        End Select

        Select Case intInputB
            Case 10 To 19
                decDiscount = 0.2D
                decPackageBTotal = (intInputB * intPackageBCost) - (intInputB * intPackageBCost * decDiscount)
            Case 20 To 49
                decDiscount = 0.3D
                decPackageBTotal = (intInputB * intPackageBCost) - (intInputB * intPackageBCost * decDiscount)
            Case 50 To 99
                decDiscount = 0.4D
                decPackageBTotal = (intInputB * intPackageBCost) - (intInputB * intPackageBCost * decDiscount)
            Case Is > 100
                decDiscount = 0.5D
                decPackageBTotal = (intInputB * intPackageBCost) - (intInputB * intPackageBCost * decDiscount)
            Case Else
                decPackageBTotal = intInputB * intPackageBCost
        End Select

        Select Case intInputC
            Case 10 To 19
                decDiscount = 0.2D
                decPackageCTotal = (intInputC * intPackageCCost) - (intInputC * intPackageCCost * decDiscount)
            Case 20 To 49
                decDiscount = 0.3D
                decPackageCTotal = (intInputC * intPackageCCost) - (intInputC * intPackageCCost * decDiscount)
            Case 50 To 99
                decDiscount = 0.4D
                decPackageCTotal = (intInputC * intPackageCCost) - (intInputC * intPackageCCost * decDiscount)
            Case Is > 100
                decDiscount = 0.5D
                decPackageCTotal = (intInputC * intPackageCCost) - (intInputC * intPackageCCost * decDiscount)
            Case Else
                decPackageCTotal = intInputC * intPackageCCost
        End Select

        decGrandTotal = decPackageATotal + decPackageBTotal + decPackageCTotal

        lblTotalOut.Text = "Package A:" & FormatCurrency(decPackageATotal) & vbCrLf & "Package B:" & FormatCurrency(decPackageBTotal) & vbCrLf _
                            & "Package C:" & FormatCurrency(decPackageCTotal) & vbCrLf & "Grand Total:" & FormatCurrency(decGrandTotal)


    End Sub



My Problem: I am using the Try parse to validate weather the inputA(B)(​C) is an integer or not, if it is an integer, i want it to move to the nested if and validate that it is a positive number, and show the error message in the label if it is not. If it is not a number at all i want it to show another message in the label.

The problem that I am getting is that even if i put in a letter for the input it does not catch it and it continues on to the select case, which comes out 0.

Is This A Good Question/Topic? 0
  • +

Replies To: Using Integer.TryParse for validation an an If statement

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Using Integer.TryParse for validation an an If statement

Posted 26 January 2012 - 04:28 PM

You will need to Exit Sub to stop execution of the routine if a non-integer value is received.

So
If Integer.TryParse(txtPackageA.Text, intInputA) Then
  intInputA = txtPackageA.Text 'This isn't need since upon success of the tryparse, the value will be stored in intInputA, otherwise, intInputA will be 0

If intInputA <= 0 Then
  lblTotalOut.Text = "You must enter a positive number for all units sold."
  Exit Sub ' Stop execution 
End If

Was This Post Helpful? 1
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Using Integer.TryParse for validation an an If statement

Posted 26 January 2012 - 04:29 PM

If you catch any problem in any case, after telling to enter number again, you have to Exit Sub. Otherwise the sub will continue executing.
So in every else condition after giving an error message to user, exit sub so user can enter number again. You can also give a focus to the text box where the mistake happens....
Was This Post Helpful? 1
  • +
  • -

#4 Borron555  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 02-March 11

Re: Using Integer.TryParse for validation an an If statement

Posted 26 January 2012 - 05:12 PM

Thanks! That worked like a charm. It was definitely one of those silly little overlooks. Appreciate the quick help though!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1