Assigning Nothing to a nullable variable sets the value to null. For more information and examples, see Nullable Value Types. If a variable is of a value type that is not nullable, assigning Nothing to it sets it to the default value for its declared type. If that type contains variable members, they are all set to their default values. The following example illustrates this for scalar types.
If a variable is of a reference type, assigning Nothing to the variable sets it to a null reference of the variable's type. A variable that is set to a null reference is not associated with any object. The following example demonstrates this:. For strings in Visual Basic, the empty string equals Nothing. The following example shows comparisons that use the Is and IsNot operators:. If you declare a variable without using an As clause and set it to Nothing , the variable has a type of Object.
A compile-time error occurs in this case when Option Strict is on and Option Infer is off. 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.
Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode. Is this page helpful? Please rate your experience Yes No. Any additional feedback? Submit and view feedback for This product This page. View all page feedback. Null is not the same as Empty , which indicates that a variable has not yet been initialized. It is also not the same as a zero-length string "" , which is sometimes referred to as a null string. Use the IsNull function to determine whether an expression contains a Null value.
This is because any expression containing a Null is itself Null and therefore False. This example uses the IsNull function to determine if a variable contains a Null. Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.
0コメント