开发者

Visual Basic Temperature Converter

开发者 https://www.devze.com 2023-03-31 03:17 出处:网络
I am new with Visual Basic and I am creating a aspx temperature converter application in Visual Studio 2010 where the user is able to input numbers into a textbox, choose what type temperature it is t

I am new with Visual Basic and I am creating a aspx temperature converter application in Visual Studio 2010 where the user is able to input numbers into a textbox, choose what type temperature it is through a dropdown listbox, and choose what temperature to convert it to from a radio button list. The problem I am having is that I am getting an error when I try convertingrr something. I get the error "Input string was not in a correct format" and "Conversion from string "F" to type 'Boolean' is not valid." I've tried doing nested if..elseif..endif statements, but when I do that it just converts the 1st if statement and nothing else. Here is the code I have written for the conversion. Any help with this would be greatly appreciated. Thank You.

 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As Sy开发者_如何学Gostem.EventArgs) Handles Button1.Click
    If (DropDownList1.SelectedValue = "F" & RadioButtonList1.SelectedValue = "F") Then
        Label1.Text = TextBox1.Text & "Fahrenheit=" & TextBox1.Text & "Fahrenheit"

    ElseIf (DropDownList1.SelectedValue = "F" & RadioButtonList1.SelectedValue = "C") Then
        Label1.Text = TextBox1.Text & "Fahrenheit=" & (TextBox1.Text * 1.8) + 32 & " Celsius"

    ElseIf (DropDownList1.SelectedValue = "F" & RadioButtonList1.SelectedValue = "K") Then
        Label1.Text = TextBox1.Text & "Fahrenheit=" & ((5 / 9) * (TextBox1.Text - 32) + 273) & " Kelvin"

    ElseIf (DropDownList1.SelectedValue = "C" & RadioButtonList1.SelectedValue = "C") Then
        Label1.Text = TextBox1.Text & "Celsius=" & TextBox1.Text & "Celsius"

    ElseIf (DropDownList1.SelectedValue = "C" & RadioButtonList1.SelectedValue = "F") Then
        Label1.Text = TextBox1.Text & " Celsius = " & (TextBox1.Text - 32) / 1.8 & " Fahrenheit"

    ElseIf (DropDownList1.SelectedValue = "C" & RadioButtonList1.SelectedValue = "K") Then
        Label1.Text = TextBox1.Text & " Celsius = " & (TextBox1.Text + 273) & " Kelvin"

    ElseIf (DropDownList1.SelectedValue = "K" & RadioButtonList1.SelectedValue = "K") Then
        Label1.Text = TextBox1.Text & "Kelvin=" & TextBox1.Text & "Kelvin"

    ElseIf (DropDownList1.SelectedValue = "K" & RadioButtonList1.SelectedValue = "F") Then
        Label1.Text = TextBox1.Text & "Kelvin=" & ((TextBox1.Text - 273) * 1.8) + 32 & "Fahrenheit"

    ElseIf (DropDownList1.SelectedValue = "K" & RadioButtonList1.SelectedValue = "C") Then
        Label1.Text = TextBox1.Text & "Kelvin=" & (TextBox1.Text - 237) & "Celsius"



    End If



End Sub


I think the two errors are:

  • you're using & in the if condition - this should be and
  • you're attempting to perform arithmetic directly on strings - you need to convert this first e.g. to a double CDbl(TextBox1.Text)

That said there's probably scope to refactor this a little: you could e.g. compute just the temperature number and then assemble the string at the bottom, testing the dropdowns there separately again so that you only have one copy each of the "Kelvin=" strings, etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消