使用AddressOf时应该如何给出参数功能?

时间:2013-10-17 15:02:38

标签: .net multithreading

请参阅以下代码:

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进行编译和执行?

1 个答案:

答案 0 :(得分:1)

它正在编译,因为你没有Option Strict。如果您打开Option Strict(您应该总是这样做的IMO)它将无法编译 - 您的功能与ThreadStartParameterizedThreadStart不兼容。如果您将参数类型更改为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