Contents of this page:
Additional
Subroutines are converted to Python functions with no return statement. Local variables in the VB subroutine are also local in the Python version. If a module global is used on the left hand side of an assignment then a Python global statement will be inserted at the head of the function. Project globals will be replaced by their fully-qualified versions.
VB:
Dim moduleGlobal1, moduleGlobal2
Sub MySub(X, Optional Y, Optional Z=20)
Dim subLocal
subLocal = X + Y + Z + moduleGlobal
moduleGlobal2 = moduleGlobal2 + 1
End Sub
MySub 1, 2
MySub 1, Z:=10
Optional arguments which are not supplied and have no defaults are initialized with the VBMissingArgument object. This object can be detected by the vbfunctions.IsMissing function to provide initialization of missing arguments within the body of the subroutine. This functionality is transparent under normal conditions, but if the subroutine manually assigns a value to the missing parameter prior to the IsMissing call then the behaviour may not match that of VB, since the vbfunctions.IsMissing function has no way to detect that the parameter was not supplied.
VB:
Dim moduleGlobal1, moduleGlobal2
Sub MySub(X, Optional Y, Optional Z=20)
Dim subLocal
If IsMissing(Y) Then Y = 12
subLocal = X + Y + Z + moduleGlobal
moduleGlobal2 = moduleGlobal2 + 1
End Sub
MySub 1, 2
MySub 1, Z:=10
VB has two argument passing schemes,
Although Python's argument passing semantics are often refered to as pass-by-reference, the actual behaviour does not always match VB's ByRef because of immutable object types and name re-binding. Although there are technical solutions for these issues, the current version of vb2Py does not make any attempt to match behaviours.
This means that the following code, although converted, does not behave the same in the Python version
VB:
Sub DoIt(x, ByVal y)
x = x + 1
y = y + 1
End Sub
x = 0
y = 0
DoIt x, y
' x is now 1, y is still 0
There are no options specific to the Sub statement.