I'm assigned a project to replace all the GoSubs
in an application based off of VBA
because this application is switching to VB.net
and GoSubs
are not supported there. Here's a simplified version of the original code:
Sub Main ()
Dim A As String
...
If ConditionX Then A = "Black"
Else A = "White"
End If
...
GoSub Execute
...
Execute:
Call BuiltInSub1 (A)
Call BuiltInSub2 (A)
'where BuiltInSubs are some predefined procedures within the application
...
Return
End Sub
I'm thinking about using a Call
to replace GoSub
as follows:
Sub Main ()
Dim A As String
If ConditionX Then A = "Black"
Else A = "White"
End If
...
Call Execute
...
End Sub
Sub Execute ()
...
Call BuiltInSub1 (A)
Call BuiltInSub2 (A)
...
End Sub
The obvious error of my modified version above is that variable A
is not declared in Sub Execute
. One option I can think of is redefining A
in Sub Execute
, but to do that, I will need to re-declare all variables used to define A
in Main, where there are quite a few.
What would be the best/most efficient way to replace GoSub
in my case?
I believe that lamda functions may have nested variable scope) in VB.net
Based on my comments above, your code should look like this:
If you do find that you need declare parameters
ByRef
, you might want to look at refactoring as a more long-term solution.