Search code examples
stringvb.netmoduleparameter-passingprogram-entry-point

Can private sub pass newly created variable to main()?


Can private sub program being in the same module as main after receiving value from main, creates new variable and pass it back to main?

This is what I am trying to do, but I am having some difficulties. For example, in testSUB below I altered the string. Can I pass extraSTRING and newSTRING back to main? Any examples would be helpful.

Module module1
    Sub main()
        Dim l As String
        Dim I As Long = 1
        Dim A As String
        testsub(l, A, I)
    End Sub

    Private Sub testSub(l As String, A As String, I As Long)
        Dim extraSTRING As String = "extraTEXT"
        Dim newSTRING As String = l & extraSTRING
    End Sub
End Module

Solution

  • To return a value you could turn your Sub into a Function:

    Private Function testFunction (ByVal arg1 As String) As String
    
        Return arg1 & " and some more text"
    
    End Function
    

    To call the above Function and assign the value returned use this code:

    Dim a As String = testFunction("some text")
    
    'Output:
    'a = "some text and some more text"
    

    Below is a screenshot of the code with the output:

    enter image description here

    Alternatively you can use ByRef:

    Specifies that an argument is passed in such a way that the called procedure can change the value of a variable underlying the argument in the calling code.

    ByRef differs slightly from ByVal:

    Specifies that an argument is passed in such a way that the called procedure or property cannot change the value of a variable underlying the argument in the calling code.

    Below is some sample code showing you the differences in action:

    Module Module1
    
        Sub Main()
            Dim a As Integer = 0
            Dim b As Integer = 0
            Dim c As Integer = 0
    
            testSub(a, b, c)
    
            'Output:
            'a = 0
            'b = 0
            'c = 3
    
        End Sub
    
        Private Sub testSub(arg1 As Integer, ByVal arg2 As Integer, ByRef arg3 As Integer)
            arg1 = 1
            arg2 = 2
            arg3 = 3
        End Sub
    
    End Module
    

    By not specifying a modifier in VB.NET (as shown with arg1 above) the compiler by default will use ByVal.

    It would be good to note here that although VB.NET uses ByVal by default if not specified, VBA does not and instead by default uses ByRef. Beware of this should you ever port code from one to the other.

    Below is a screenshot of the code with the output:

    enter image description here

    Using your code as an example:

    Sub main()
        Dim l As String
        Dim A As String
        Dim I As Long = 1
        testSub(l, A, I)
    End Sub
    

    To pass the variables l, A and I and have their value changed you would change your method to use the modifier ByRef.

    Private Sub testSub(ByRef l As String, ByRef A As String, ByRef I As Long)
        l = "TEXT"
        A = "extra" & l
        I = 100
    End Sub
    

    Below is a screenshot of the code with the output:

    enter image description here