Subs, as you will find in any programming that use them, are there because they are NOT supposed to return a value. It's not their intent. They are supposed to be there to perform a task based upon the given inputs.
If you want to return a value, you need to use a Function. Which has all the same functionality of a sub, but can return a value.
HOWEVER, you can declare variables as shared/global so that a Sub can act on them... however, this can make it hard to follow the data around when it comes to debugging.
Example:
Sub Main
Dim x As Integer = 5
Dim y As Integer = 2
Call MathSub(x,y)
FunctionResult = MathFunction(x,y)
MsgBox("This is in main sub. The result of the function call is: " & FunctionResult)
End Sub
'note the sub does not have a type
Sub MathSub(var1 as Integer, var2 as Integer)
Dim resultvar As Integer
resultvar = var1 + var2
MsgBox("I am in the sub MathSub. The result I give is: " & resultvar)
End Sub
'note the function has to have a type
Function MathFunction(var1 as Integer, var2 as Integer) As Integer
Dim result As Integer
result = var1-var2
Return result
End Function
Example using global variables:
See: for more info on access modifiers:
https://msdn.microsoft.com/en-us/library/76453kax.aspx
'Need to put all of the rule inside of a class in order to be able to use the shared variable.
Class TestShared
'This shared access modifier keyword makes the information accessible to all subs/functions/properties in this class
Shared oResult As Integer
Sub Main
oResult = 0
Dim x As Integer = 5
Dim y As Integer = 2
MsgBox("Before sub call result: " & vbLf & oResult)
PerformMath(x, y)
MsgBox("After sub call result: " & vbLf & oResult)
End Sub
'note the sub does not have a type
Sub PerformMath(var1 as Integer, var2 as Integer)
oResult = var1 + var2
End Sub
End Class
--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.