开发者

VB check for Null Reference when passing ByRef

I have a function that accepts a String by reference:

Function Foo(ByRef input As String)

If I call it like this:

Foo(Nothing)

I want it to do something different than if I call it like this:

Dim myString As String = Nothing
Foo(myString)

Is it possible to detect this difference in the way the method is called in VB .NET?

Edit

To clarify why the heck I would want to do this, I have two methods:

Function Foo()
  Foo(Nothing)
End Function

Function Foo(ByRef input As String)
  'wicked awesome logic here,  hopefully
End Function

All the logic is in the second overload, but I want to perform a differen开发者_开发问答t branch of logic if Nothing was passed into the function than if a variable containing Nothing was passed in.


No. In either case, the method "sees" a reference to a string (input) which is pointing to nothing.

From the method's point of view, these are identical.


You could add a Null Reference check either:

1) prior to calling the function

If myString IsNot Nothing Then 
     Foo(myString)
End If

2) or inside the function

Function Foo(ByRef input As String)
    If input Is Nothing Then
        Rem Input is null
    Else
        Rem body of function
    End If
End Function
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜