在Visual Studio 2017 Visual Basic中,我收到"End of statement expected"错误



在下面的代码中,我得到一个"预期的语句结束"和"'文本'不是字符串的成员"错误:

Public Class Form1
Private Sub btnFtoC_Click(sender As Object, e As EventArgs) Handles btnFtoC.Click
Try
Dim f As Decimal CDec(txtF.Text)
Dim c As Decimal
Dim txtC As String
c = 5 / 9 * (f - 32)
txtC.Text = CStr(c)
Catch ex As Exception
End Try
End Sub
End Class

您已使用Dim txtC As String将 txtC 声明为String,因此没有txtC.Text。 (也许您打算填充文本框?

我假设txtF和txtC是文本框。您需要测试 txtF 中的输入以查看它是否是有效的Decimal

Private Sub btnFtoC_Click(sender As Object, e As EventArgs) Handles btnFtoC.Click
Dim f As Decimal
If Not Decimal.TryParse(txtF.Text, f) Then
MessageBox.Show("Please enter a valid number")
Return
End If
Dim c = CDec(5 / 9 * (f - 32))
txtC.Text = CStr(c)
End Sub

最新更新