请参阅以下代码:
Public Sub MyFunction1(ByVal CodeNo As Integer)
Dim thread As New Thread(AddressOf MyFunction2)
thread.Start()
End Sub
Private Sub MyFunction2(ByVal CodeNo As Integer)
Debug.Print CodeNo
End Sub
如何将参数值提供给MyFunction2
?
为什么编译器允许将CodeNo
设置为0
进行编译和执行?
答案 0 :(得分:1)
它正在编译,因为你没有Option Strict。如果您打开Option Strict(您应该总是这样做的IMO)它将无法编译 - 您的功能与ThreadStart
或ParameterizedThreadStart
不兼容。如果您将参数类型更改为Object
,那么它很好 - 您可以将值Start
传递给函数。简短而完整的例子:
Option Strict On
Imports System
Imports System.Threading
Public Class Test
Public Shared Sub Main()
Dim thread As New Thread(AddressOf Foo)
thread.Start("hello")
thread.Join()
End Sub
Private Shared Sub Foo(ByVal value As Object)
Console.WriteLine(value)
End Sub
End Class