Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 使用不在Visual Basic中工作的函数的简单表单_Vb.net - Fatal编程技术网

Vb.net 使用不在Visual Basic中工作的函数的简单表单

Vb.net 使用不在Visual Basic中工作的函数的简单表单,vb.net,Vb.net,我第一次使用函数,并尝试创建一个简单的BMI计算器,该计算器显示BMI值和带有消息框的相应类别。虽然VisualBasic没有指出我的代码有任何问题,但当我单击表单上的按钮时,什么也没有发生。由于VB没有给我任何关于问题是什么的提示,我不知道从哪里开始寻找 Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim heig

我第一次使用函数,并尝试创建一个简单的BMI计算器,该计算器显示BMI值和带有消息框的相应类别。虽然VisualBasic没有指出我的代码有任何问题,但当我单击表单上的按钮时,什么也没有发生。由于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
公共类表单1
私有子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
尺寸高度为Double=TextBox1.Text
将重量调整为Double=TextBox2.Text
端接头
函数Bmicalc(ByVal身高加倍,ByVal体重加倍)加倍
双倍体质量指数
体重指数=(体重/(身高*身高))*703
Bmicalc=bmi
返回体重指数
端函数
函数bmiCategory(ByVal bmi作为双精度,ByVal category作为字符串)作为字符串
如果bmi<18.5,则
类别=“体重不足”
如果体重指数>=18.5且体重指数>25
类别=“正常”
如果体重指数>=25且体重指数<30
类别=“超重”
如果体重指数>30,则
类别=“肥胖”
如果结束
类别=类别
退货类别
MessageBox.Show(“您的bmi为:&”&bmi&“类别为:&”&category)
端函数
末级
公共类表格1
私有子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
尺寸高度为Double=TextBox1.Text
将重量调整为Double=TextBox2.Text
将返回字符串设置为字符串
returnString=bmicontegory(Bmicalc(身高、体重),“”)
端接头
函数Bmicalc(ByVal身高加倍,ByVal体重加倍)加倍
双倍体质量指数
体重指数=(体重/(身高*身高))*703
'如果您想返回一个值,只需按照下面的方式执行即可
返回体重指数
端函数
函数bmiCategory(ByVal bmi作为双精度,ByVal category作为字符串)作为字符串
如果bmi<18.5,则
类别=“体重不足”
如果体重指数>=18.5且体重指数>25
类别=“正常”
如果体重指数>=25且体重指数<30
类别=“超重”
如果体重指数>30,则
类别=“肥胖”
如果结束
MessageBox.Show(“您的bmi为:&”&bmi&“类别为:&”&category)
返回类别的通知在这里!
端函数
末级

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

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
Private子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
'将来,您还应该验证用户输入。
双倍体质量指数=BmiCalc(TextBox1.Text,TextBox2.Text)
作为字符串的Dim类别=bmi类别(bmi)
“最好在活动中显示您的信息,而不是在功能上
MessageBox.Show(“您的bmi为:&”&bmi.ToString&“类别为:&”&category)
端接头
函数BmiCalc(ByVal身高加倍,ByVal体重加倍)加倍
双倍体质量指数=(体重/(身高*身高))*703
返回体重指数
”“或者只是
返回(重量/(高度*高度))*703
端函数
“因为您实际上不需要对变量类别执行任何操作,所以您不需要这样做。
“只需为每种情况填写一份申报表即可。
“我想你可能也想知道这个案例。
函数bmicography(ByVal bmi作为Double)作为字符串
如果bmi<18.5,则
返回“重量不足”
如果体重指数>=18.5且体重指数<25
返回“正常”
如果体重指数>=25且体重指数<30
返回“超重”
其他的
返回“肥胖”
如果结束
端函数

函数
方法/Sub
(在VB.net中)几乎相同,只是它们是返回值(或对象、结构或无)的方法,如果您熟悉C#,假设您在C#中只有函数,而C#方法是带有
void
关键字(无返回类型)的方法

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

    ' 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())
您可以使用
函数
方法
,而无需将输出分配给变量。有些情况下它是有用的


Bmicalc函数: 类别功能:
您应该注意的一些要点…

  • 如果我在
    TextBox1.Text中输入“lol”怎么办?创建应用程序时,应假定lambda用户是一个2岁的孩子。。。(或者至少这是学校教给我们的),比如检查
    TextBox1.Text
    是否包含有效的正数值文本。。我也可以输入负数,或者选择TextBox1的内容,点击键盘上的“删除”,然后点击按钮1

        Dim height As Double = TextBox1.Text ' <- "lol" throws an exception...
    
  • 分部:

        bmi = (weight / (height * height)) * 703
        ' what if heigth = 0
        ' You'll have a DivisionByZero Exception...
    
  • “…虽然Visual Basic没有指出我的代码有任何问题…”我以前也这么认为,直到我在每个vb文件内容的顶部添加了这些内容:

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Dummy
        ' ... blah blah blah
        ' ...
    
  • 为了使事情更简单,我在VB.net中定义
    函数时使用了前缀:Getblahblah。(这里是这些函数的修改版本)

  • 这让我想到最后一点:你确定StackOverflow(SO)真的能帮你吗?SO的目的是帮助解决一些真正不方便的问题,这些问题很少有文档记录,甚至以前从未尝试过。您应该查看vb.Net文档,了解以下几点:

    • vb.Net中的函数是如何工作的
    • “Return”关键字的作用是什么
    • 执行返回blahblah后是否有代码
  • 在大多数情况下,SO不会给出问题的答案,而这些问题的答案可以通过在google中键入该问题来轻松找到

    我并不想表现得粗鲁或居高临下,也不想看起来聪明,我相对来说还不熟悉一般的编程。一些新的SO用户/
        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
    
        Dim height As Double = TextBox1.Text ' <- "lol" throws an exception...
    
        bmi.ToString()
    
        bmi = (weight / (height * height)) * 703
        ' what if heigth = 0
        ' You'll have a DivisionByZero Exception...
    
    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Dummy
        ' ... blah blah blah
        ' ...
    
    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
    
    Public Enum bmiLevel
        UnderWeight
        Normal
        OverWeight
        Obese
    End Enum
    
    ' and use one value of that enumeration as an output for bmiCategory
    Public Function bmiCategory(ByVal bmi As Double) As bmiLevel
        If bmi < 18.5 Then
            Return bmiLevel.UnderWeight
        ElseIf bmi < 25 Then
            Return bmiLevel.Normal
        ElseIf ' ....
            ' ....
    
    ' then depending on the value of category, 
    ' display the appropriate String value in the appropriate Culture
    ' somewhere in other part of your code, like in Button1.Click handling...
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' ...
        ' ...
        result = "Your bmi is: " & bmi.ToString() & " - The category is: "
        Select Case category
            Case bmiLevel.UnderWeight:
                result = result + "Underweight"
            Case bmiLevel.Normal:
                result = result + "Normal"
            ' ...
        End Select
        MessageBox.Show(result)
    End Sub