使用在Visual Basic中不起作用的函数的简单表单

时间:2014-10-22 23:41:24

标签: vb.net

我是第一次使用函数,并尝试使用消息框创建一个显示BMI值和相应类别的简单BMI计算器。虽然Visual Basic没有表明我的代码有任何问题,但当我单击表单上的按钮时没有任何反应。由于VB没有给我任何关于问题的提示,我不知道从哪里开始寻找。

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim height As Double = TextBox1.Text
        Dim weight As Double = TextBox2.Text
    End Sub
    Function Bmicalc(ByVal height As Double, ByVal weight As Double) As Double
        Dim bmi As Double
        bmi = (weight / (height * height)) * 703
        Bmicalc = bmi
        Return bmi
    End Function
    Function bmiCategory(ByVal bmi As Double, ByVal category As String) As String
        If bmi < 18.5 Then
            category = "Underweight"
        ElseIf bmi >= 18.5 And bmi > 25 Then
            category = "Normal"
        ElseIf bmi >= 25 And bmi < 30 Then
            category = "Overweight"
        ElseIf bmi > 30 Then
            category = "Obese"
        End If
        bmiCategory = category
        Return category
        MessageBox.Show("Your bmi is: " & "" & bmi & "The category is: " & "" & category)
    End Function
End Class

3 个答案:

答案 0 :(得分:0)

 Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim height As Double = TextBox1.Text
    Dim weight As Double = TextBox2.Text
    Dim returnString as String
    returnString=bmiCategory(Bmicalc(height,weight),"")
End Sub
Function Bmicalc(ByVal height As Double, ByVal weight As Double) As Double
    Dim bmi As Double
    bmi = (weight / (height * height)) * 703
    'if you want to return a value,just do it like below
    Return bmi
End Function
Function bmiCategory(ByVal bmi As Double, ByVal category As String) As String
    If bmi < 18.5 Then
        category = "Underweight"
    ElseIf bmi >= 18.5 And bmi > 25 Then
        category = "Normal"
    ElseIf bmi >= 25 And bmi < 30 Then
        category = "Overweight"
    ElseIf bmi > 30 Then
        category = "Obese"
    End If
    MessageBox.Show("Your bmi is: " & "" & bmi & "The category is: " & "" & category)
    Return category'Notice here!
    End Function
   End Class

答案 1 :(得分:0)

只是您的代码的一点建议和反射。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'In the future, you should validate user input as well.
    Dim bmi As Double = BmiCalc(TextBox1.Text, TextBox2.Text)
    Dim category As String = BmiCategory(bmi)

    'It's  better to have your message displayed at event instead of Funtion
    MessageBox.Show("Your bmi is: " & "" & bmi.ToString & "The category is: " & "" & category)
End Sub

Function BmiCalc(ByVal height As Double, ByVal weight As Double) As Double
    Dim bmi As Double = (weight / (height * height)) * 703
    Return bmi
    'OR Just 
    Return (weight / (height * height)) * 703
End Function

'Since you dont actually perform any action with variable Category, you dont need that.
'Just place a return for each condition.
'I assume you might wan to know about Select Case too.
Function BmiCategory(ByVal bmi As Double) As String
    If bmi < 18.5 Then
        Return "Underweight"
    ElseIf bmi >= 18.5 And bmi < 25 Then
        Return "Normal"
    ElseIf bmi >= 25 And bmi < 30 Then
        Return "Overweight"
    Else
        Return "Obese"
    End If
End Function

答案 2 :(得分:0)

FunctionsMethods/Sub(在VB.net中)几乎相同,只是它们是返回值的方法(或对象,结构或Nothing)如果您熟悉C#,请假设您在C#中只有功能,而C#方法是{ {1}}关键字(无返回类型)

要在大多数情况下具有含义(在VB.Net中),您应该将函数的返回类型分配给相同类型的变量:

void

您可以使用 ' Block that calls the Function Dim myVar As Double = Bmicalc(height, weight) ' Bmicalc returns a Double.. ' then use myVar as required ' as it hosts the output value of Bmicalc previously called ' ... MessageBox.Show(myVar.ToString()) 之类的Functions,而无需将输出分配给变量。在某些情况下,它很有用。


Bmicalc功能:

Methods

bmiCategory功能:

    Function Bmicalc(ByVal height As Double, ByVal weight As Double) As Double
        Dim bmi As Double
        bmi = (weight / (height * height)) * 703
        ' Bmicalc = bmi : Remove this : not required.

        Return bmi ' this Function returns the value of bmi (Double)
    End Function

Button1的

点击事件时您的按钮应触发的内容:

    Function bmiCategory(ByVal bmi As Double) As String
        ' Remove the Category Paremeter declaration, 
        ' and move it inside this Function :

        Dim category As String

        If bmi < 18.5 Then
            category = "Underweight"
        ElseIf bmi >= 18.5 And bmi < 25 Then ' Beware : you had a typo here "> 25 !!!
            category = "Normal"
        ElseIf bmi >= 25 And bmi < 30 Then
            category = "Overweight"
        ElseIf bmi > 30 Then
            category = "Obese"
        End If

        ' bmiCategory = category <- Remove this : not required.

        Return category ' this Function returns the value of category

        ' MessageBox.Show("Your bmi is: " & "" & bmi & "The category is: " & "" & category)
        ' Remove the MessageBox : Functions are rarely usefull 
        ' when they constantly pop a DialogBox...
    End Function

您应该注意的一些要点......

  1. 如果我输入&#34; lol&#34;在 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim height As Double = TextBox1.Text ' get the height - OK Dim weight As Double = TextBox2.Text ' get the weight - OK ' Calculate bmi... Dim bmi As Double = Bmicalc(height, weight) ' OK ' format the result... Dim result As String = "Your bmi is: " & "" & bmi & "The category is: " & "" & bmiCategory(bmi) ' show the result... MessageBox.Show(result) ' Don't be used to mix core code (the Bmicalc and bmiCategory Functions) ' and user interface (The Button and the resulting DialogBox) End Sub ?当你创建一个应用程序时,你应该假设lambda用户是一个2岁的孩子......(或者至少是那个在学校教我们的东西),比如检查TextBox1.Text是否包含有效的正数字文字 ..我也可以输入负数,或者选择TextBox1的内容,点击&#34;删除&#34;在键盘上然后单击Button1 ...

    TextBox1.Text
  2. DotNet为 Dim height As Double = TextBox1.Text ' <- "lol" throws an exception... 类型提供有用的转换。你应该随时使用它们(我没有在上面的代码中应用这个规则,但你应该这样做)我知道这很烦人,但是当我不得不更新应用程序时,这节省了我很多时间。几个月或几年后。清洁和自我解释。

    String
  3. 分部:

        bmi.ToString()
    
  4. &#34; ...虽然Visual Basic没有表明我的代码存在任何问题...&#34; 我曾经有同样的想法,直到我在每个vb的顶部文件内容:

        bmi = (weight / (height * height)) * 703
        ' what if heigth = 0
        ' You'll have a DivisionByZero Exception...
    
  5. 为了简化操作,我在VB.net中定义Option Strict On Option Explicit On Option Infer Off Public Class Dummy ' ... blah blah blah ' ... 时使用了前缀 获取 blahblah 。 (这里是函数的重写版本)

    Functions
  6. 返回Public Function GetBmi(ByVal Height As Double, ByVal Weight As Double) As Double If Height > 0 Then Return Math.Abs(Weight) * 703 / (Height * Height) Else Throw New ArgumentException("Negative or null Height is not allowed.") End If End Function Public Function GetCategory(ByVal Bmi As Double) As String Select Case True ' Pick one test in sequence and return a value if True... ' similar behaviour as If/ElseIf...EndIf Case bmi < 18.5: Return "Underweight" Case bmi < 25: Return "Normal" Case bmi < 30: Return "Overweight" Case Else: Return "Obese" End Select ' Note that you could have discard the lower bound test (ElseIf bmi >= 25) ' in your original If/ElseIf...End If block aswell. End Function 的函数...您应该知道String是一种为CPU付出很多代价的类型。如果您的应用程序使用多语言怎么办?您必须将String功能的&#34;超重&#34; 输出转换为&#34; Surpoids&#34; 为法语例如,文化...更好地使用枚举,你不会想到?喜欢:

    bmiCategory
  7. 引导我到最后一点:您确定StackOverflow(SO)能真正帮助您吗? SO的目的是帮助解决一些非常不方便的问题,这些问题很少被记录,甚至从未尝试过。您应该查看vb.Net文档中的一些内容:

    • 函数如何在vb.Net中运行?
    • &#34;返回&#34;关键字呢?
    • 在执行返回blahblah后执行代码
  8. 在大多数情况下,SO并没有给出问题的答案,可以通过在谷歌中输入这个问题来轻松找到答案...

    没有意思是粗鲁或居高临下,或者看起来也很聪明,我对SO来说相对较新,只是平均而且#34;编程水平。一些新的SO用户/成员被标记或低估并不了解SO的主要目标,最终认为SO只适用于专家......它只是效率 :具体问题的具体答案。如果一个问题要求整本书的答案是“正确的”,那么就无法实现使用最少的代码行来解决问题的工作解决方案的目的。因此,与网上大量的等等不同......