使用子变量编码温度程序

时间:2014-03-27 23:39:00

标签: vb.net

我正在尝试我制作的这个程序,它涉及将输入的数字(温度)更改为摄氏或华氏。它使用2个单选按钮(radCelsius,radFahrenheit),一个转换按钮(btnConvert),一个退出按钮(btnExit)和一个输入按钮(txtTemp),以及"结果"(lblResult)这个问题我是'我有点是当我点击转换按钮时它没有给我任何数字。这是代码:

Option Explicit On
Option Strict On
Option Infer Off

Public Class frmMain
Private Sub CalcTemp(ByVal dblCel As Double,
                     ByVal dblFah As Double,
                      ByVal dblTemp As Double)
    'calculates Temperature

    If radCelsius.Checked Then

        dblCel = (dblTemp * 9 / 5 + 32)
    Else
        dblFah = (dblTemp - 32 * 5 / 9)
    End If


End Sub
Private Sub ClearLabel(ByVal sender As Object, ByVal e As System.EventArgs
                       ) Handles txtTemp.TextChanged, radCelsius.Click, radFahrenheit.Click
    lblResult.Text = String.Empty
End Sub

Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
    Me.Close()
End Sub

Private Sub txtTemp_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTemp.Enter
    txtTemp.SelectAll()
End Sub

Private Sub btnConvert_Click(sender As Object, e As System.EventArgs) Handles btnConvert.Click
    Dim dblCelsius As Double
    Dim dblFahrenheit As Double
    Dim dblTemp As Double

    Double.TryParse(radCelsius.Checked.ToString, dblCelsius)
    Double.TryParse(radFahrenheit.Checked.ToString, dblFahrenheit)
    dblTemp = Val(txtTemp.Text)
    Call CalcTemp(dblCelsius, dblFahrenheit, dblTemp)

    lblResult.Text = txtTemp.ToString()
End Sub
End Class

我已经尝试将txtTemp转换为字符串(然后将CStr添加到txtTemp)但是当我点击"转换"时它仍然没有给我任何数字。按钮。任何见解将不胜感激。谢谢

2 个答案:

答案 0 :(得分:1)

这里有一些评论,对我跳出的第一件事就是你试图将RadioButton Checked值转换为Double,Double.TryParse(radCelsius.Checked.ToString,dblCelsius)这两行没有任何成就,可以摆脱它。第二个是你的CalcTemp子例程正常工作,但你没有使用它的任何值。我会把它变成一个函数并将你的计算结果返回给你的调用函数。

修改后的代码

Private Function CalcTemp(ByVal dblTemp As Double)
    'calculates Temperature
    Dim result As Double
    If radCelsius.Checked Then

        result = (dblTemp * 9 / 5 + 32)
    Else
        result = ((dblTemp - 32) * 5 / 9) 'Note changed parenthesis for your math to be correct
    End If

    Return result

End Function

Private Sub btnConvert_Click(sender As Object, e As System.EventArgs) Handles btnConvert.Click
    Dim dblTemp As Double
    lblResult.Text = CalcTemp(dblTemp).ToString()
End Sub

使用ByRef的修改示例

Private Sub btnConvert_Click(sender As Object, e As System.EventArgs) Handles btnConvert.Click
    Dim dblTemp As Double
    dblTemp = Val(txtTemp.Text)
    CalcTemp(dblTemp)
    lblResult.Text = dblTemp.ToString()
End Sub

Private Sub CalcTemp(ByRef dblTemp As Double)
    'calculates Temperature
    If radCelsius.Checked Then
        dblTemp = (dblTemp * 9 / 5 + 32)
    Else
        dblTemp = ((dblTemp - 32) * 5 / 9)
    End If

End Sub

答案 1 :(得分:0)

ByVal更改为ByRef以获取计算温度的子程序,因为看起来这就是您打算将这些参数用作的内容。

试试这个:

Private Sub CalcTemp(ByRef dblCel As Double,
                     ByRef dblFah As Double,
                      ByVal dblTemp As Double)
    'calculates Temperature

    If radCelsius.Checked Then

        dblCel = (dblTemp * 9 / 5 + 32)
    Else
        dblFah = (dblTemp - 32 * 5 / 9)
    End If