所以错误是BC42353(函数ValidateInputFields
没有在所有代码路径上返回一个值。你错过了Return
语句吗?)我两次得到这个错误。我在感叹号上写了感叹号。
Public Class Form1
Private decWholesaleCost As Decimal
Private decMarkuppercent As Decimal
Private Function ValidateInputFields() As Boolean
If Not Decimal.TryParse(txtWholesale.Text, decWholesaleCost) Then
lblMessage.Text = "The wholesale cost must be numeric"
Return False
End If
If Not Decimal.TryParse(txtMarkup.Text, decMarkuppercent) Then
lblMessage.Text = "Markup percentage must be numeric"
Return False
End If
! End Function
Function CalculateRetailPrice(ByVal decWholesaleCost As Decimal,
ByVal decMarkupPercent As Decimal) As Decimal
Dim decRetailPrice As Decimal
decRetailPrice = decWholesaleCost + (decWholesaleCost * decMarkupPercent)
! End Function
Private Sub BtnGetRetail_Click(sender As Object, e As EventArgs) Handles btnGetRetail.Click
Dim decRetailPrice As Decimal
lblMessage.Text = String.Empty
If ValidateInputFields() Then
decRetailPrice = CalculateRetailPrice(decRetailPrice, decMarkuppercent)
lblRetail.Text = decRetailPrice.ToString("c")
End If
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
答案 0 :(得分:1)
您的代码未返回任何值。
Private Function ValidateInputFields() As Boolean
If Not Decimal.TryParse(txtWholesale.Text, decWholesaleCost) Then
lblMessage.Text = "The wholesale cost must be numeric"
Return False
End If
If Not Decimal.TryParse(txtMarkup.Text, decMarkuppercent) Then
lblMessage.Text = "Markup percentage must be numeric"
Return False
End If
Return True '--need to add this line
End Function
如果在.NET中编写一个非void类型的方法,则必须返回一个值。您的代码不返回值,这就是编译器产生错误的原因。
Private Function ValidateInputFields() As Boolean
If Not Decimal.TryParse(txtWholesale.Text, decWholesaleCost) Then
lblMessage.Text = "The wholesale cost must be numeric"
Return False
End If
If Not Decimal.TryParse(txtMarkup.Text, decMarkuppercent) Then
lblMessage.Text = "Markup percentage must be numeric"
Return False
End If
--there is no return value from function when above if condition fails
End Function
请在方法结束时添加Return True
;这应该可以解决你的问题。