Results 1 to 2 of 2

Thread: Help with error in try catch statements

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2009
    Posts
    13

    Exclamation Help with error in try catch statements

    I am working on a school project and I need to insert a try catch statement. When I insert the code below
    Try
    firstTextBox.Text

    Catch ex As Exception

    MsgBox("Please enter a numeric value in the first box.")
    firstTextBox.Clear()

    End Try

    I get the error that says "Property access must assign to the property or use its value." Can anyone tell me what this error message means and how to correct it? I have attached my full code below in case that helps.

    Thank you for your help in advance!

    Public Class MainForm

    Private Sub pythRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pythRadioButton.CheckedChanged

    'Change Labels to Pythagorean Labels
    firstLabel.Text = "A="
    secondLabel.Text = "B="
    thirdLabel.Text = "C="
    End Sub

    Private Sub cylRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cylRadioButton.CheckedChanged

    'Change Labels to Cylinder Labels
    firstLabel.Text = "R="
    secondLabel.Text = "H="
    thirdLabel.Text = "V="
    End Sub

    Private Sub calcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calcButton.Click

    Try
    firstTextBox.Text

    Catch ex As Exception
    MsgBox("Please enter a numeric value in the first box.")
    firstTextBox.Clear()
    End Try


    Try
    secondTextBox.Text

    Catch ex As Exception
    MsgBox("Please enter a numeric value in the second box.")
    secondTextBox.Clear()
    End Try

    If pythRadioButton.Checked Then 'If Pythagorean is checked

    'Declare Variables
    Dim a As Decimal
    Dim b As Decimal
    Dim c As Decimal

    'Get the values
    Decimal.TryParse(firstTextBox.Text, a)
    Decimal.TryParse(secondTextBox.Text, b)

    'Do the math
    c = a ^ 2 + b ^ 2

    'Output Answer
    answerLabel.Text = Math.Sqrt(c)

    Else 'If Pythagorean isn't checked, then Cylinder is

    'Declare variables
    Dim h As Decimal
    Dim r As Decimal
    Dim v As Decimal

    'Get Values
    Decimal.TryParse(firstTextBox.Text, h)
    Decimal.TryParse(secondTextBox.Text, r)

    'Do Cylinder Calculations'
    v = (r ^ 2) * 3.14159 * h

    'Output Answer
    answerLabel.Text = v
    End If

    firstTextBox.Focus()
    End Sub

    Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click

    Me.Close()
    End Sub

    Private Sub firstTextBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles firstTextBox.Enter
    firstTextBox.SelectAll()
    End Sub

    Private Sub firstTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles secondTextBox.TextChanged, firstTextBox.TextChanged
    answerLabel.Text = ""
    End Sub

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    pythRadioButton.Checked = True
    End Sub


    Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click
    firstTextBox.Clear()
    secondTextBox.Clear()

    End Sub
    End Class

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Help with error in try catch statements

    it means you're using it wrong:

    vb Code:
    1. firstTextBox.Text = "something"

    or:

    vb Code:
    1. dim something as string = firstTextBox.Text

    to check for a number using try, catch:

    vb Code:
    1. Try
    2. dim decNumber as decimal = cdec(firstTextBox.Text)
    3.  
    4. Catch ex As Exception
    5. MsgBox("Please enter a numeric value in the first box.")
    6. firstTextBox.Clear()
    7. End Try

    if its successful, the number will be stored in decNumber, otherwise it'll display the msgbox

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width