Discussion Locked

Help! Pass variable back to Sub Main

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi:
(Forgive my lack of programming terminology) I have a main sub that calls a sub (SubA), passing parameter values to that sub (SubA). SubA calls a Function that returns a value to SubA, SubA exits sub and returns to Sub Main, and now the variable is empty again. I want to be able to pass the value back to the main sub, so I can use the value in my code. Without using a class, how can I achieve this. Thanks much!

7 replies

by timfox

I'm not sure what language you are using. The situation could apply to VBA, so I'll assume this.

Returning values from functions is easy, see below:

Sub Main()
dim num1 as integer
dim num2 as integer
dim sumofnums as integer

sumofnums = GetSum (num1, num2)

if sumofnums < 0 then 'function failed
msgbox "Result is negative, ie. not good! Could not add " & num1 & " with " & num2
else
msgbox "Sum equals: " & sumofnums
end if

End Main

Public Function GetSum (thisNum as integer, thatNum as integer) as integer
GetSum = thisNum + thatNum
End Function

Note - no error coding or checking included, although I am checking that the sum is a positive number. You could return a string to use other result-checking characters if you prefer ;-)
Just be careful when leaping through functions and passing variables and check for validity of results etc.

Without knowing the dev tool, I don't know how else to help - hope you get something from the above code, it should at least be a start (-there are many ways to go about this).

Side note after re-reading your post...
As your second sub is calling a function that is(?) returning a value back to SubA, then I would look at the responsibility of each sub and the associated functions. Perhaps you should ask the Main to fetch the value? Wherever you're variable is - check the scope of it (ie. Where can it be referenced) Perhaps you need a global variable? (-sorry to end with questions)

TimFox

by sl-campbell

When you say "Without using a class..." then I am assuming that the proceedures are in a Module and not a class module or Form (A form is a class).

You could use a public variable in the module - not always the best idea unless the variable is tobe used through-out the program). Or you could make sure that the variable is passed from proceedure to proceedure by reference (byRef):

Put the next line in the declarations section of a public module:
Public sTestPubVar As String

and then drop all of the following code at the bottom of the same module:

Sub TestSub()

Dim sTest As String

TestSub1 sTest

Debug.Print sTest
Debug.Print sTestPubVar
'in this example sTest should hold the value "HI" and sTestPubVar the
'string: "Public Variable Set In TestSub2"

End Sub

Sub TestSub1(ByRef sTestVar1 As String)
TestSub2 sTestVar1
End Sub

Sub TestSub2(ByRef sTestVar2 As String)
sTestVar2 = "HI"
sTestPubVar = "Public Variable Set In TestSub2"
End Sub'============================

'Function Test ====================
Sub TestFunc()

Dim sTest1 As String
Dim sTest2 As String

sTest2 = TestFunc1(sTest1)

Debug.Print sTest1
Debug.Print sTest2
Debug.Print sTestPubVar
'in this example sTest1 should hold the value "HI"
'and sTest2 should return "BYE"
'and sTestPubVar the string: sTestPubVar = "Public Variable Set In TestFunc2"

End Sub
Function TestFunc1(ByRef sTestVar1 As String) As String
TestFunc1 = TestFunc2(sTestVar1)
End Function

Function TestFunc2(ByRef sTestVar2 As String) As String

sTestVar2 = "HI"
TestFunc2 = "BYE"
sTestPubVar = "Public Variable Set In TestFunc2"
End Function
'============================

Now open the immediate window and call first the sub: "TestSub" and then the sub "TestFunc" (Enter the word TestSub or TestFunc and hit the Enter key)

by rsp05

PLEASE NOTE: THAT I MEANT TO ACCEPT ANSWER #2 from sl-campbell@gmx.de and apparently it was a technical blunder of why rejected........I hereby award all points to sl-campbell@gmx.de in spirit. (If there is a way, please do so @ TECHREPUBLIC) thx

This discussion is closed to new replies.