Ahm*_*eed 15

编辑:根据这篇博客文章,这应该可以在VB.NET 10中实现.如果您正在使用它,那么您可以:

Public Sub DoSomething(Optional ByVal someInteger As Integer? = Nothing)
    Console.WriteLine("Result: {0} - {1}", someInteger.HasValue, someInteger)
End Sub

' use it
DoSomething(Nothing)
DoSomething(20)
Run Code Online (Sandbox Code Playgroud)

对于VB.NET 10以外的版本:

你的要求是不可能的.您应该使用可选参数,或者可以为空.此签名无效:

Public Sub DoSomething(Optional ByVal someInteger As Nullable(Of Integer) _
                        = Nothing)
Run Code Online (Sandbox Code Playgroud)

您将收到此编译错误:"可选参数不能具有结构类型."

如果您正在使用可空,则在不想传递值的情况下将其设置为Nothing.选择以下选项:

Public Sub DoSomething(ByVal someInteger As Nullable(Of Integer))
    Console.WriteLine("Result: {0} - {1}", someInteger.HasValue, someInteger)
End Sub
Run Code Online (Sandbox Code Playgroud)

要么

Public Sub DoSomething(Optional ByVal someInteger As Integer = 42)
    Console.WriteLine("Result: {0}", someInteger)
End Sub
Run Code Online (Sandbox Code Playgroud)


Mar*_*ett 6

你不能,所以你必须做一个过载,而不是:

Public Sub Method()
  Method(Nothing) ' or Method(45), depending on what you wanted default to be
End Sub

Public Sub Method(value as Nullable(Of Integer))
  ' Do stuff...
End Sub
Run Code Online (Sandbox Code Playgroud)