0

请参阅以下代码:

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

4

1 回答 1

1

它正在编译,因为您没有启用 Option Strict。如果您打开 Option Strict (IMO 您几乎应该总是这样做),它将无法编译 - 您的函数与ThreadStartor不兼容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
于 2013-10-17T15:07:58.323 回答