2015-05-11 71 views
0

我在這段代碼中遇到問題,因爲我沒有返回任何東西!在VB.net中返回函數

它的工作正如我想它沒有返回,但當我返回它崩潰。

你能幫忙嗎?

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 

我在網上查了一下,但我不明白他們使用的方法,這就是爲什麼它更好地問一個直接的問題,謝謝。

回答

0

Public Function Clear() As Boolean

您定義的功能誰將會在任何時候都返回一個布爾值的函數。

但是在你的代碼中你根本沒有分配一個返回值。

如果你不想返回值,只是改變你的函數名稱: Public Sub Clear()

如果你想返回一個值,你可以這樣做:

Public Function Clear() As Boolean 
    return False 
End function 

返回值

當你創建一個新的函數/方法,你可以,如果你想要的FUNC選擇方法返回一個值。這是可選的,但非常有用。

假設你想寫一個函數來計算2個值的總和。你會做什麼,就是編寫函數,你傳遞給它的兩個值這種方式,並且它返回的總和:

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

的返回值將是一個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。

+0

將它指定爲布爾值是什麼意思? –

+0

你走了,回答改善了。 – Jordumus

+0

我無法投票我需要15代表抱歉 –