1
votes

In the following code, I get an "End of statement expected" and "'Text' is not a member of string" errors:

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
2
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 - Paolo Barone
You're working with temperature so use Double (scientific values) and not Decimal (monetary values). - Enigmativity
Also, learn now not to ever write Catch ex As Exception. You should only ever catch specific exceptions, that you can't code your way out of, and that you can meaningfully handle. You should rarely write an exception handler. Too many people use them too often and they just end up creating buggy code. - Enigmativity
Try Dim f As Decimal = CDec(txtF.Text) - Enigmativity
Also, make sure you have Option Strict On and Option Explicit On while coding in VB.NET. They'll make you a better coder. - Enigmativity

2 Answers

0
votes

You've declared txtC as a String using Dim txtC As String so there is no txtC.Text. (Perhaps you were intending to populate a textbox?)

0
votes

I assume txtF and txtC are text boxes. You need to test the input in the txtF to see if it is a valid 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