我的函式沒有回傳值。我如何使這項作業?我想將值回傳給 btt1process => browser 和 btt2url => url ,不幸的是它沒有回傳
s2 = "Chrome"
s3 ="www.google.com"
代碼
Public btt1task, btt2task, btt3task as string
Public btt1process, btt2process, btt3process as string
Public btt1url, btt2url, btt3url as string
代碼按鈕:
Dim userMsgBrowser As String = "Chrome"
Dim userMsgAdressName As String = "www.google.com"
If btt1task = "yes" Then
TaskNewProcessKey(btt1task, btt1process, btt1url)
ElseIf btt2task = "yes" Then
TaskNewProcessKey(btt2task, btt2process, btt2url)
End If
代碼:
Public Function TaskNewProcessKey(s1, s2, s3 As String) As String
Dim userMsgBrowser As String = "Chrome"
Dim userMsgAdressName As String = "www.google.com"
If s1 = "yes" Then
s1 = "ProcessURL"
s2 = InputBox("what is your browser? please enter the process browser name, or press ok for to continue with the default browser (Chrome.exe), set by default", "Process Browser Name",)
If s2.Length < 1 Then
s2 = "Chrome"
End If
s3 = InputBox("what is your adress url?", "Process Adress URL",)
If s3 < 1 Then
s3 = "www.google.com"
End If
End If
Return s1
Return s2
Return s3
End Function
測驗:
Messagebox.Show(btt1task)
Messagebox.Show(btt1process)
Messagebox.Show(btt1url)
預期輸出:
Messagebox.Show(btt1task) => "yes"
Messagebox.Show(btt1process) => "Chrome"
Messagebox.Show(btt1url) => "www.google.com"
uj5u.com熱心網友回復:
您似乎對如何Functions作業有誤解,可能應該就該主題做一些額外的閱讀。需要注意的幾點
- 當您呼叫 a 時
Function,Return Value如果您打算使用它,則意味著將其分配給一個變數。
在您當前的代碼中,您忽略Return Value了Function.
Dim userMsgBrowser As String = "Chrome"
Dim userMsgAdressName As String = "www.google.com"
If btt1task = "yes" Then
TaskNewProcessKey(btt1task, btt1process, btt1url)
ElseIf btt2task = "yes" Then
TaskNewProcessKey(btt2task, btt2process, btt2url)
End If
要捕獲回傳值,您可以執行以下操作
Dim userMsgBrowser As String = "Chrome"
Dim userMsgAdressName As String = "www.google.com"
Dim result As String 'result will be used to store the return value of the function
If btt1task = "yes" Then
result = TaskNewProcessKey(btt1task, btt1process, btt1url)
ElseIf btt2task = "yes" Then
result = TaskNewProcessKey(btt2task, btt2process, btt2url)
End If
- 該
Return陳述句導致立即退出Function程序。雖然您可以在程序中的任何位置使用任意數量的 return 陳述句,但執行的第一個陳述句將退出程序。
在您當前的代碼中,只會回傳 s1 的值。其他值(s2 和 s3)Function在回傳 s1 時不會作為出口回傳
Public Function TaskNewProcessKey(s1, s2, s3 As String) As String
Dim userMsgBrowser As String = "Chrome"
Dim userMsgAdressName As String = "www.google.com"
If s1 = "yes" Then
s1 = "ProcessURL"
'omitted code here
End If
Return s1 'The first return statement to be executed exits the procedure
Return s2 'this will never be executed
Return s3 'this will never be executed
End Function
- 使用 return 陳述句只能回傳一個物件
'This function returns a single String
Public Function TaskNewProcessKey(s1, s2, s3 As String) As String '<-String return type
'omitted code here
Return s1 'The first return statement to be executed exits the procedure
Return s2 'this will never be executed
Return s3 'this will never be executed
End Function
為了讓您的函式回傳多個值,您需要創建一個自定義物件來保存這些值或使用陣列或元組之類的東西
'This function returns a Tuple containing 3 Strings
Public Function TaskNewProcessKey(s1 As String, s2 As String, s3 As String) As (s1 As String, s2 As String, s3 As String) '<-Tuple return type
'omitted code here
Return (s1, s2, s3)
End Function
或者,如果您查看
ByValvsByRef,您可以考慮使用ByRef引數來獲取從程序中回傳的值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/402839.html
標籤:
上一篇:Python期末復習資源
下一篇:如果選擇索引,則串列視圖條件
