Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`x <> Nothing` vs `x IsNot Nothing`

in VB is there any situation whereby x = Nothing is not the same as x is Nothing?

Also, x <> Nothing vs x IsNot Nothing

I'm thinking that they are purely identical.. but just wanted to be sure.

like image 728
Pacerier Avatar asked May 20 '11 05:05

Pacerier


People also ask

What does <> mean in Visual Basic?

<> in VB.NET means "not equal to". It can be used with the normal oprands as well as in comparision with the items when compared with the datas fetched with the data reader (from database). Follow this answer to receive notifications.

Is not nothing in C#?

IsNothing is intended to work on reference types. It returns True if the expression represents an object variable that currently has no object assigned to it; otherwise, it returns False.

Is VB nothing same as null?

For non-nullable value types, Nothing in Visual Basic differs from null in C#. In Visual Basic, if you set a variable of a non-nullable value type to Nothing , the variable is set to the default value for its declared type.

What does nothing mean in VBA?

The Nothing keyword is used to disassociate an object variable from an actual object. Use the Set statement to assign Nothing to an object variable. For example: VB Copy. Set MyObject = Nothing.


1 Answers

There's a difference with empty strings, which do count as Nothing with simply "=" or "<>" but don't count as Nothing for Is / IsNot:

Public Class Test
    Public Shared Sub Main()
        Dim x As String = ""
        Console.WriteLine(x = Nothing)   ' True
        Console.WriteLine(x Is Nothing)  ' False
    End Sub
End Class
like image 117
Jon Skeet Avatar answered Oct 23 '22 19:10

Jon Skeet