Partilhar via


Nada (Visual Basic)

Represents the default value of any data type.

Comentários

Assigning Nothing to a variable sets it to the default value for its declared type. If that type contains variable members, they are all set to their default values. O exemplo a seguir ilustra isso para tipos escalares.

Module Module1
    Public Structure testStruct
        Public name As String
        Public number As Short
    End Structure

    Sub Main()

        Dim ts As testStruct
        Dim i As Integer
        Dim b As Boolean

        ' The following statement sets ts.name to Nothing and ts.number to 0.
        ts = Nothing

        ' The following statements set i to 0 and b to False.
        i = Nothing
        b = Nothing

        Console.WriteLine("ts.name: " & ts.name)
        Console.WriteLine("ts.number: " & ts.number)
        Console.WriteLine("i: " & i)
        Console.WriteLine("b: " & b)

    End Sub

End Module

Se a variável de um tipo de referência, um valor de Nothing significa que a variável não é associado a qualquer objeto. A variável tem um valor nulo. The following example demonstrates this.

Module Module1

    Sub Main()

        Dim testObject As Object
        ' The following statement sets testObject so that it does not refer to
        ' any instance.
        testObject = Nothing

        Dim tc As New TestClass
        tc = Nothing
        ' The fields of tc cannot be accessed. The following statement causes 
        ' a NullReferenceException at run time. (Compare to the assignment of
        ' Nothing to structure ts in the previous example.)
        'Console.WriteLine(tc.field1)

    End Sub

    Class TestClass
        Public field1 As Integer
        ' . . .
    End Class
End Module

Para variáveis teste de referência e o tipo que permite valor nulo para Nothing valores, use o Is operador ou a IsNot operador. Comparações que usam o sinal de igual, por exemplo, someVar = Nothing, sempre avaliada como Nothing. O exemplo a seguir mostra as comparações que usam o Is e IsNot operadores.

Module Module1
    Sub Main()

        Dim testObject As Object
        testObject = Nothing
        ' The following statement displays "True".
        Console.WriteLine(testObject Is Nothing)

        Dim tc As New TestClass
        tc = Nothing
        ' The following statement displays "False".
        Console.WriteLine(tc IsNot Nothing)

        Dim n? As Integer
        ' The following statement displays "True".
        Console.WriteLine(n Is Nothing)
        n = 4
        ' The following statement displays "False".
        Console.WriteLine(n Is Nothing)
        n = Nothing
        ' The following statement displays "False".
        Console.WriteLine(n IsNot Nothing)

    End Sub

    Class TestClass
        Public field1 As Integer
        Dim field2 As Boolean
    End Class
End Module

For more information and examples, see Tipos de valor anulável (Visual Basic).

When you assign Nothing to an object variable, it no longer refers to any object instance. If the variable had previously referred to an instance, setting it to Nothing does not terminate the instance itself. The instance is terminated, and the memory and system resources associated with it are released, only after the garbage collector (GC) detects that there are no active references remaining.

Consulte também

Referência

Instrução Dim (Visual Basic)

Operador Is (Visual Basic)

Operador IsNot (Visual Basic)

Conceitos

Vida útil de objeto: Como os objetos são criados e destruídos (Visual Basic)

Tempo de vida no Visual Basic

Tipos de valor anulável (Visual Basic)