private sub 可以将新创建的变量传递给 main() 吗?
Can private sub pass newly created variable to main()?
私有子程序是否可以在从主程序接收到值后与主程序在同一模块中,创建新变量并将其传回主程序?
这就是我想要做的,但我遇到了一些困难。
例如,在下面的 testSUB 中,我更改了字符串。我可以将 extraSTRING 和 newSTRING 传递回 main 吗?任何示例都会有所帮助。
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
到 return 一个值你可以把你的 Sub
变成 Function
:
Private Function testFunction (ByVal arg1 As String) As String
Return arg1 & " and some more text"
End Function
要调用上面的 Function
并分配值 returned 使用此代码:
Dim a As String = testFunction("some text")
'Output:
'a = "some text and some more text"
下面是带有输出的代码的屏幕截图:
或者您可以使用 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
与 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.
下面是一些示例代码,向您展示了操作上的差异:
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
通过不在 VB.NET
中指定 modifier
(如上面 arg1
所示)编译器默认情况下将使用 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.
下面是带有输出的代码的屏幕截图:
以您的代码为例:
Sub main()
Dim l As String
Dim A As String
Dim I As Long = 1
testSub(l, A, I)
End Sub
要传递变量 l
、A
和 I
并更改它们的值,您需要更改方法以使用修饰符 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
下面是带有输出的代码的屏幕截图:
私有子程序是否可以在从主程序接收到值后与主程序在同一模块中,创建新变量并将其传回主程序?
这就是我想要做的,但我遇到了一些困难。 例如,在下面的 testSUB 中,我更改了字符串。我可以将 extraSTRING 和 newSTRING 传递回 main 吗?任何示例都会有所帮助。
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
到 return 一个值你可以把你的 Sub
变成 Function
:
Private Function testFunction (ByVal arg1 As String) As String
Return arg1 & " and some more text"
End Function
要调用上面的 Function
并分配值 returned 使用此代码:
Dim a As String = testFunction("some text")
'Output:
'a = "some text and some more text"
下面是带有输出的代码的屏幕截图:
或者您可以使用 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
与 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.
下面是一些示例代码,向您展示了操作上的差异:
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
通过不在 VB.NET
中指定 modifier
(如上面 arg1
所示)编译器默认情况下将使用 ByVal
.
It would be good to note here that although
VB.NET
usesByVal
by default if not specified,VBA
does not and instead by default usesByRef
. Beware of this should you ever port code from one to the other.
下面是带有输出的代码的屏幕截图:
以您的代码为例:
Sub main()
Dim l As String
Dim A As String
Dim I As Long = 1
testSub(l, A, I)
End Sub
要传递变量 l
、A
和 I
并更改它们的值,您需要更改方法以使用修饰符 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
下面是带有输出的代码的屏幕截图: