Home > Net >  Function string not return inputbox value
Function string not return inputbox value

Time:01-03

my function does not return the value. how do i make this work? i want to return value to btt1process => browser , and btt2url => url , unfortunately it does not return

s2 = "Chrome" s3 = "www.google.com"

Code

Public btt1task, btt2task, btt3task as string
Public btt1process, btt2process, btt3process as string
Public btt1url, btt2url, btt3url as string

Code button:

    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

Code:

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

testing:

Messagebox.Show(btt1task)
Messagebox.Show(btt1process)
Messagebox.Show(btt1url)

Expected output:

Messagebox.Show(btt1task) => "yes"
Messagebox.Show(btt1process) => "Chrome"
Messagebox.Show(btt1url) => "www.google.com"

CodePudding user response:

You seem to have a misunderstanding of how Functions work and should probably do some additional reading on the topic. A couple things to note

  • When you call a Function, the Return Value is meant to be assigned to a variable if you have intentions of using it.

In your current code, you are ignoring the Return Value of the 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

To capture the return value you would do something like

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
  • The Return statement causes an immediate exit from a Function procedure. While you can have any number of return statements anywhere within the procedure, the first one that is executed will exit the procudure.

In your current code, only the value of s1 will be returned. The other values (s2 & s3) are not returned as the Function exits when returning 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
  • Only one object can be returned using the return statement
'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

In order for your function to return multiple values, you would either need to create a custom object that holds those values or utilize something like an array or tuple

'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.

  • Related