Is there a VB.NET expression that *always* yields null?

后端 未结 4 1730
走了就别回头了
走了就别回头了 2021-02-19 19:18

We all know that VB\'s Nothing is similar, but not equivalent, to C#\'s null. (If you are not aware of that, have a look at this answer first.)

相关标签:
4条回答
  • 2021-02-19 19:37

    The simple answer is, no. There is no expression in VB.NET that only returns null. As you know, when the compiler parses a command using ternary operator, it infers the output type based on the two inputs. If one of the two inputs is Nothing, it must rely solely on the other parameter. Therefore, the "right" way to do it in VB.NET is to first cast the other parameter to Object, thereby forcing the output of the operation to be an Object:

    Dim o As Object = If(myBool, DirectCast(5, Object), Nothing)
    

    If, however, you really need an in-line expression which, itself, always evaluates to null, you could always do it by invoking a lambda expression, like this:

    Dim o As Object = If(myBool, 5.0, (Function() Nothing).Invoke())
    

    That syntax should work in any situation and would always result in Nothing rather than potentially resulting in default value.

    0 讨论(0)
  • 2021-02-19 19:39

    The first answer I gave missed some points, but this should do it:

    Dim o As Object = If(myBool, 5, DirectCast(Nullable.GetUnderlyingType(GetType(Integer)), Object))
    

    This uses the fact that Nullable.GetUnderlyingType will return a null reference if you pass it a type which isn't a nullable value type - which Integer isn't. Other alternatives exist, such as Type.GetElementType(), or perhaps GetType(Object).BaseType.

    I've checked that this works with multiple different types for the second operand.

    It's slightly annoying that you have to cast to Object... I'm still working on alternatives for that...

    0 讨论(0)
  • 2021-02-19 19:45

    System.DBNull.Value is most likely what you're after.

    Expansion, from Tech Republic:

    ... The following C# code determines if a string value is null:

    string sTest = "Test"; if (sTest == null) { Console.WriteLine("sTest is Null"); }
    

    This code works without any problems with C#, but there's no VB.NET equivalent of the null keyword. Instead, VB.NET uses the Nothing keyword. The following code demonstrates its use:

    Dim sTest As String
    If (sTest Is Nothing) Then
        Console.WriteLine("sTest is Null") 
    End If
    

    Another area of confusion stems from the fact that VB.NET doesn't treat null and Nothing as equals; consequently, a VB.NET programmer may have to check for both values.

    The variation in support may cause confusion, but a developer rarely develops in both languages simultaneously. On the other hand, Microsoft provides a uniform method for working with null values: The base System.Convert namespace includes the DBNull object.

    DBNull

    The use of the DBNull class indicates the absence of a known value. While the Microsoft documentation says it's typically in a database application, you may also use it with any type of data.

    If (sTest.Equals(System.DBNull.Value) Then
        Console.WriteLine("sTest is Null")
    End  If 
    

    -- verbatim from Working with Null values in the .NET Framework

    0 讨论(0)
  • 2021-02-19 20:01

    You can create one yourself (pick a different name for the function if you like):

    Private Function ValueOrNull(expression As Boolean, value As Object) As Object
      If expression Then Return value
      Return Nothing
    End Function
    

    Usage:

    Dim myBool As Boolean
    Dim o As Object = ValueOrNull(myBool, 5)
    

    Works with any type, including your example with 5.0.

    0 讨论(0)
提交回复
热议问题