拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 函式字符串不回传输入框值

函式字符串不回传输入框值

白鹭 - 2022-01-25 2210 0 0

我的函式没有回传值。我如何使这项作业?我想将值回传给 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 时FunctionReturn Value如果您打算使用它则意味着将其分配给一个变量。

在您当前的代码中,您忽略Return ValueFunction.

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自变量来获取从程序中回传的值。

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *