Sebastian Roman
Sebastian Roman

Reputation: 3

Visual basic less than 0

Im trying to make a program that when i put a value less than 0, in a label something like " negative numbers cant be used i have this

Public Class Form1 'Sebastian roman. Perimeter, 10/1/2014


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

        Try
            Dim intSide1 As Integer = txtSide1.Text
            Dim intSide2 As Integer = txtSide2.Text
            Dim intSide3 As Integer = txtSide3.Text
            Dim intSide4 As Integer = txtSide4.Text
            Dim intTotal As Integer = intSide1 + intSide2 + intSide3 + intSide4
            lblMessage.Text = intTotal.ToString("#,###.##")
        Catch ex As Exception
            MessageBox.Show("Incorrect Input. Enter a numeric value.")
        End Try

    End Sub
End Class

Yes i have to use the try catch method and i need help for this

Upvotes: 0

Views: 1965

Answers (2)

Joel Etherton
Joel Etherton

Reputation: 37533

This sounds an awful lot like homework, but you really need to convert your integers properly and then perform actual comparisons.

Public Class Form1 'Sebastian roman. Perimeter, 10/1/2014

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

        Try
           ' Relies on the GetIntegerInput method to throw exceptions
           ' for invalid entries
            Dim intSide1 As Integer = GetIntegerInput(txtSide1.Text)
            Dim intSide2 As Integer = GetIntegerInput(txtSide2.Text)
            Dim intSide3 As Integer = GetIntegerInput(txtSide3.Text)
            Dim intSide4 As Integer = GetIntegerInput(txtSide4.Text)
            Dim intTotal As Integer = intSide1 + intSide2 + intSide3 + intSide4
            lblMessage.Text = intTotal.ToString("#,###.##")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Function GetIntegerInput(ByVal input as String) As Integer)
        Dim returnValue as Integer

        ' Will attempt a proper try parse. AndAlso will short circuit
        ' the comparison so a failure in TryParse will not perform the
        ' the second evaluation. In either case, an actual exception is
        ' thrown with your invalid numeric message 
        If (Not Int32.TryParse(input, returnValue) AndAlso returnValue < 0) Then
            Throw New ArgumentException("Incorrect input. Enter a proper numeric value.")
        End If

        Return returnValue
    End Function
End Class

Upvotes: 2

boomoto
boomoto

Reputation: 311

Well a integer can be negative...

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

    Try
        Dim intSide1 As Integer = txtSide1.Text
        Dim intSide2 As Integer = txtSide2.Text
        Dim intSide3 As Integer = txtSide3.Text
        Dim intSide4 As Integer = txtSide4.Text
        Dim intTotal As Integer = intSide1 + intSide2 + intSide3 + intSide4
        lblMessage.Text = intTotal.ToString("#,###.##")
        //New code
        if intSide1 < 0 or intSide2 < 0 or intSide3 < 0 intSide4 < 0 Then
         MessageBox.Show("Incorrect Input. Negative number not valid")
        end if
    Catch ex As Exception
        MessageBox.Show("Incorrect Input. Enter a numeric value.")
    End Try



End Sub

Upvotes: 0

Related Questions