Try Catch Code

How to write Try Catch when the user inputs text and you need Integers

Page 1 of 1

2 Replies - 9212 Views - Last Post: 12 November 2010 - 03:46 AM Rate Topic: -----

#1 krajr3518  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-November 10

Try Catch Code

Posted 11 November 2010 - 07:04 PM

I'm creating aprogram that calculates the speed of sound and I'm not sure how to code the Try Catch, so it can catch the problem if the user inputs letters, instead of numbers. Please help me to learn how to code for this exception, so a message box will display the request ("Please try again and only enter numbers.")

' This system will calculate the speed of sound, in the user's coice of medium (Air, Water, or Steel)
    ' The user will choose one of three radio buttons that each correspond to one of the three medium choices.
    ' The user will then enter a distance (in feet), and press the "Calculate" button.
    ' The calculated answer will be given in feet/second, and will appear in a label.

    Dim intSpeedOfSoundAir As Integer 'Speed of sound in Air
    Dim intSpeedOfSoundWater As Integer ' Speed of sound in Water
    Dim intSpeedOfSoundSteel As Integer ' Speed of sound in Steel

    Private Property NumberAsObject As Short


    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        ' Check to see if data has been entered in the text box
        If txtDistanceIn.Text = "" Then
            MsgBox("Please enter a value", MsgBoxStyle.Exclamation)
            txtDistanceIn.Focus()
            Return
        End If

        ' Check to make sure that the user has entered numeric data in the text box
        Try
            txtDistanceIn = Int(NumberAsObject) As Object
            txtDistanceIn = CInt(With.ObjectAsExpression)AsInteger(ToString())
            "Conversion from string \"abc\" to type 'Double' is not valid."
        Catch ex As InvalidCastException
            MessageBox.Show("Please try again and only enter numbers.")
            Return
        End Try

        ' Check the Distance entered. It must be more than 0. 
        If (txtDistanceIn.Text < 1) Then
            MsgBox("Your distance must be more than zero" & Val(txtDistanceIn.Text), MsgBoxStyle.Exclamation)
            txtDistanceIn.Text = ""
            txtDistanceIn.Focus()
            Return
        End If

        ' When a radio button is checked, the proper speed of sound will be used to calculate the answer
        If radAir.Checked = True Then
            MessageBox.Show("Your time in seconds: " &
                CStr(FormatNumber(CDec(txtDistanceIn.Text) / 1100)), ("Distance Traveled")

        ElseIf radWater.Checked = True Then
            MessageBox.Show("Your time in seconds: " &
                CStr(FormatNumber(CDec(txtDistanceIn.Text) / 4900)), ("Distance Traveled")

        ElseIf radSteel.Checked = True Then
            MessageBox.Show("Your time in seconds: " &
               CStr(FormatNumber(CDec(txtDistanceIn.Text) / 16400)), ("Distance Traveled")
        End If

    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

        'End the application by closing the window
        Me.Close()
    End Sub
    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

        ' clearing the text box and output label
        txtDistanceIn.Clear()
    End Sub
End Class


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

And VB.NET is NOT VB6...moved to VB.NET

This post has been edited by JackOfAllTrades: 12 November 2010 - 05:05 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Try Catch Code

#2 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 328
  • View blog
  • Posts: 1,990
  • Joined: 26-March 09

Re: Try Catch Code

Posted 12 November 2010 - 01:48 AM

VB6 uses the onerror statement to handle exceptions.

Looking at your code, I think you'd be better asking a mod to move it to the VB.NET forum, also, try wrapping your code in the "code tags", it will make it much easier to read.
Was This Post Helpful? 0
  • +
  • -

#3 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 469
  • View blog
  • Posts: 4,280
  • Joined: 25-March 09

Re: Try Catch Code

Posted 12 November 2010 - 03:46 AM

on other hand you dont need try-catch for this you can use parse:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dblNumber As Double
        If Double.TryParse(TextBox1.Text, dblNumber) Then
            'cast succesful'
        Else
            'error not a number'
            MessageBox.Show("Error pls input numbers only")
        End If
    End Sub


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1