函数字符串不是 return 输入框值

Function string not return inputbox value

我的函数没有 return 值。我如何使这项工作?我想 return 值给 btt1process => browser 和 btt2url => url ,不幸的是它没有 return

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"

您似乎对 Functions 的工作原理有误解,可能应该对 topic 做一些额外的阅读。 有几点需要注意

  • 当您调用 Function 时,如果您打算使用它,Return Value 将被分配给一个变量。

在您当前的代码中,您忽略了 FunctionReturn Value

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

要捕获 return 值,您可以执行类似

的操作
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 的值会被 returned。其他值(s2 和 s3)未 returned,因为 returning s1

Function 退出
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 语句 returned 一个对象
'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

为了让您的函数 return 多个值,您需要创建一个自定义对象来保存这些值,或者使用数组或元组之类的东西

'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

Alternatively, if you look into ByVal vs ByRef, you can consider using ByRef parameters to get your values returned from the procedure.