我有一个通过引用接受字符串的函数:
Function Foo(ByRef input As String)
如果我这样称呼它:
Foo(Nothing)
我希望它做一些与我这样称呼它不同的事情:
Dim myString As String = Nothing
Foo(myString)
是否可以通过在 VB .NET 中调用该方法的方式来检测这种差异?
编辑
为了澄清为什么我想这样做,我有两种方法:
Function Foo()
Foo(Nothing)
End Function
Function Foo(ByRef input As String)
'wicked awesome logic here, hopefully
End Function
所有逻辑都在第二个重载中,但如果将
Nothing
传递到函数中,而不是传入变量 contains Nothing
,我想执行不同的逻辑分支。
不。无论哪种情况,该方法都会“看到”对字符串 (
input
) 的引用,但该字符串不指向任何内容。
从方法的角度来看,它们是相同的。
您可以添加空引用检查:
1) 调用函数之前
If myString IsNot Nothing Then
Foo(myString)
End If
2) 或在函数内部
Function Foo(ByRef input As String)
If input Is Nothing Then
Rem Input is null
Else
Rem body of function
End If
End Function