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! :wave:
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
Re: Help with error in try catch statements
it means you're using it wrong:
vb Code:
firstTextBox.Text = "something"
or:
vb Code:
dim something as string = firstTextBox.Text
to check for a number using try, catch:
vb Code:
Try
dim decNumber as decimal = cdec(firstTextBox.Text)
Catch ex As Exception
MsgBox("Please enter a numeric value in the first box.")
firstTextBox.Clear()
End Try
if its successful, the number will be stored in decNumber, otherwise it'll display the msgbox