1
votes

I'm hoping someone can clarify what I'm doing wrong here. I am trying to make a form with 2 labels - Celsius and Fahrenheit with 2 respective textbooks for the Value and 1 button - Convert to display the conversion of Celsius into Fahrenheit. The following Code I use keeps running me into Strict Options errors, 2 for Option Strict On disallows implicit conversions from 'Object' to 'String' and 2 for Option Strict On disallows implicit conversion from 'String' to 'Double'. I can't seem to find a way to please the strict options.

 Private Sub btnConvert_Click(sender As Object, e As EventArgs) Handles btnConvert.Click
    Dim celsius As String
    Dim answer As String
    Dim fahrenheit As String

    celsius = txtCelsius.Text
    fahrenheit = txtFahrenheit.Text

    If String.IsNullOrEmpty(txtFahrenheit.Text) Then
        answer = celsius * 9 / 5 + 32
        txtFahrenheit.Text = Int(answer)
    End If
    If String.IsNullOrEmpty(txtCelsius.Text) Then
        answer = (fahrenheit - 32) * 5 / 9
        txtCelsius.Text = Int(answer)
2
Tip: Your celsius, fahrenheit and answer variables should be numeric types, not strings. That will make your errors easier to fix - Matt Wilko

2 Answers

0
votes

You have many implicit conversions that can be fixed/made explicit:

You have implicit conversion in the celsius * 9 / 5 + 32and (fahrenheit - 32) * 5 / 9. celcius and farhenheit are strings, but you're using it as a numbers.

You also have when you put the result into answer: answer = celsius * 9 / 5 + 32
answer is a string but you're assigning the result of a calculation. It properly should be a double or similar datatype and not a string.

And then when you put Int(answer) into a textfield. first answer is still a string, but Int() takes a number (double) if I remember right. Then you take that result and put automatically into a string: txtCelsius.Text = Int(answer)

0
votes

With Option Strict On

You need to do conversion yourself

I have edited your code, Try this

        Dim celsius As String
        Dim answer As String
        Dim fahrenheit As String

        celsius = txtCelsius.Text
        fahrenheit = txtFahrenheit.Text

        If String.IsNullOrEmpty(txtFahrenheit.Text) Then
            answer = CStr(CDbl(celsius) * 9 / 5 + 32)
            txtFahrenheit.Text = answer
        End If
        If String.IsNullOrEmpty(txtCelsius.Text) Then
            answer = CStr((CDbl(fahrenheit) - 32) * 5 / 9)
            txtCelsius.Text = answer
        End If