在VB.net中返回函数

时间:2015-05-11 12:15:09

标签: vb.net function

我在这段代码中遇到问题,因为我没有回复任何东西!

它的工作正如我想要它而不返回但是当我返回它时崩溃。

你能帮帮忙吗?

Public Function Clear() As Boolean


    'resetting the variuabels to be ready for next Order


    ZYZZ = "0"
    VAT = "0"
    MAX = "0"

    'this code is 3 peices 

    'This is sitting ctrl as a new control
    Dim ctrl As Control = Me.GetNextControl(Me, True)



    '1- is to look for text box and change them to 0 after the button is pressed
    Do
        If TypeOf ctrl Is TextBox Then
            ctrl.Text = "0"
        End If
        ctrl = Me.GetNextControl(ctrl, True)

    Loop Until ctrl Is Nothing


    '2- it clears the list box
    OrderListBox.Items.Clear()

    'And uncheck the check boxes
    LoyalCheckBox.Checked = False
    TakeAwayCheckBox.Checked = False


    'Finally it resets the Price, VAT, Total in the UI 

    Label6.Text = "£" & " " & "0"
    Label7.Text = "£" & " " & "0"
    Label8.Text = "£" & " " & "0"

    'Clearing the array to prepare for next order

    arr.Clear()



End Function

此代码中同样存在问题!

Private Function calculate() As Boolean


    '=================================='

    ZYZZ = 0

    For i As Integer = 0 To arr.Count - 1

        ZYZZ = ZYZZ + arr(i)

    Next i

    '=================================='


    '=================================='

    If LoyalCheckBox.Checked = True Then

        CardT = ZYZZ * card

    Else
        CardT = "0"

    End If

    If TakeAwayCheckBox.Checked = True Then

        TAF = ZYZZ * TA


    Else
        TAF = "0"
    End If

    '=================================='

    VAT = ZYZZ * cVAT

    MAX = ZYZZ - (CardT + TAF) + VAT


    Label6.Text = "£" & " " & ZYZZ

    Label7.Text = "£" & " " & VAT

    Label8.Text = "£" & " " & MAX



End Function

我在网上查了一下,但我不明白他们使用的方法,这就是为什么更好地问一个直接的问题,谢谢。

1 个答案:

答案 0 :(得分:0)

Public Function Clear() As Boolean

您将函数定义为将始终返回布尔值的函数。

但是在您的代码中,您根本不会指定返回值。

如果您不想返回值,只需将您的函数名称更改为: Public Sub Clear()

如果要返回值,则必须这样做:

Public Function Clear() As Boolean
     return False
End function

返回值

创建新函数/方法时,可以选择是否希望该函数/方法返回值。这是可选的,但非常有用。

假设您要编写一个计算2个值之和的函数。你要做的是写函数,你传递2个值,并返回总和:

Public Function SumOfValues(ByVal value1, ByVal value2) As Integer

返回值为integer,定义为As Integer

然后你做一些代码魔术,在你的函数结束时,你返回你想要返回的任何东西,在我的例子中:2个值的总和:

Public Function SumOfValues(ByVal value1, ByVal value2) As Integer
    Dim Sum As Integer = Value1 + Value2
    Return Sum
End Function

所以,现在,只要你想知道2个值的总和,你就可以这样做:

Dim answerOfLife As Integer = SumOfValues(21,21)

answerOfLife在此示例中为42。