Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.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 在tip计算器中使用函数_Vb.net - Fatal编程技术网

Vb.net 在tip计算器中使用函数

Vb.net 在tip计算器中使用函数,vb.net,Vb.net,我必须在作业中使用函数,但我不确定它应该是什么样子。这就是我现在拥有的。。。帮助写一行vb中声明的函数表示错误 Dim BillTotal, TipAmount, PerPerson, TipDecimal, Total As Double Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub Butt

我必须在作业中使用函数,但我不确定它应该是什么样子。这就是我现在拥有的。。。帮助写一行vb中声明的函数表示错误

Dim BillTotal, TipAmount, PerPerson, TipDecimal, Total As Double
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click



    MsgBox("Tip Amount: " & TipAmount & Chr(13) & "Per Person: " & PerPerson & Chr(13) & "Total: " & Total & Chr(13))

End Sub


Function Total(ByVal BillTotal As Double) As Double
    BillTotal = mtbBill.Text
    TipAmount = (mtbTip.Text / 100) * BillTotal
    TipDecimal = (mtbTip.Text / 100)
    PerPerson = ((BillTotal * (1 + TipDecimal)) / mtbSharedBy.Text)
    Total = BillTotal * (1 + TipDecimal)
End Function
  • 将Chr(13)替换为vbNewLine-可读性更高
  • 添加了String.Format以更好地格式化MessageBox中的文本。 使其更具可读性
  • 将Total函数修改为不接受任何参数,因为它的返回值只是对UI元素值的计算



  • Dim BillTotal、TipAmount、每人、TipDecimal、总计为双倍

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    End Sub
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    
    MsgBox(String.Format("Tip Amount: {0}{1} per person{2}{1} Total:{3}{1}", TipAmount, vbNewLine, PerPerson,Total()))
        ' Here I replaced the code with String.Format making it easier to read.
        ' You specify {0}, {1}, {2}, {n} where the number is the order of the parameter.
        ' Then at the end of your string you specify the parameter name such as TipAmount, vbNewLine(which is essentially Chr(13), PerPerson, and Total
    
    End Sub
    
    
    Function Total() As Double ' Removed the parameter passed into the function as it's not required. 
        BillTotal = mtbBill.Text
        TipAmount = (mtbTip.Text / 100) * BillTotal
        TipDecimal = (mtbTip.Text / 100)
        PerPerson = ((BillTotal * (1 + TipDecimal)) / mtbSharedBy.Text)
        Return( BillTotal * (1 + TipDecimal))
    End Function
    

    你有问题吗?它有用吗?启用选项Strict。始终“帮助编写vb中声明的函数表示错误的行”。那是一串毫无意义的词。如果您的IDE告诉您有错误,并且您希望得到帮助,请确切地告诉我们错误发生的位置以及错误消息的确切内容。您需要同时启用Option Strict和Option Strict吗?出于什么原因?关闭Option Strict是不好的做法。关闭它可以隐藏所有类型的错误。