A NullReferenceException is caused by accessing something on a Nothing value. First example. This program assigns a String variable to Nothing. This is not an empty string. It is a String object that does not exist. We compare it to Nothing in an If-statement. Part 1 We use the "Is" operator for testing references against Nothing. Part 2 We use the IsNothing function to see if a reference is equal to Nothing. This is another syntax form for the same thing.
This exception not NothingReferenceException is provoked by accessing a member on Nothing. If a reference may be Nothing, we must test before using its members. When a Module or Class is created, the fields within it are automatically set to Nothing. You never need to assign them to Nothing at creation time. In C you cannot set null to value types, so for reference types String equivalent for C null in vb. The final version of the accepted answer correctly answers the question.
However, Fabio added some useful information to his answer, but it has a number of incorrect information and concepts. So this answer is an attempt to merge the two answers in a more complete one with the correct information.
First of all, Nothing in VB is equivalent to null in C , so your understanding of that is correct, at least partially because that's only one use of the Nothing keyword. It's been reflected throughout the MSDN literature, although it's been fading recently and being replaced with the word null in many comments. Your confusion arises from the behavior and features of VB which differ from C and programmers who are coming from other languages and not experienced in VB get confused by the results.
As stated in the accepted answer, the question is considered a string comparison by VB because one of the operands is a string.
Numeric comparisons treat Nothing as 0. String comparisons treat Nothing as "" an empty string. Now, why is that happening in VB? The first usage is equivalent to null in C as I mentioned above. The second usage is equivalent to the default T operator in C. In these lines, VB uses Nothing as default T to assign the default value to the variable:. Finally, when comparing variables of different types, both VB and C convert one operand to match the type of the other operand.
For example, if you compare double and integer values. Both VB and C will return true for this line:. However, VB and C differ in what types can be converted to what.
Also, the empty string "" and null are treated the same in string comparisons in VB, but not in C. This makes things easier in most cases, which is something known about VB in general.
So you can easily test if your string has no value like this:. Both lines will return the same result because VB treats Nothing as "" for comparison. In C , you cannot do that and you have to test for both like this:. Obviously, VB is easier and shorter than C in this case which is the most common one.
However, this comes at the cost of losing some control. Also although VB is easier in the previous point, C is clearer. That's why from code clarity perspective, this line can be used instead in both languages with the same result:.
Nothing in VB varies by the objects type: - Reference types equate to null - Value types equate to the default value; which for a nullable type is null.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. In managed code, like the. NET Framework, initial values are 0 or Nothing. NET program that uses module-level field Imports System.
Nothing can be used with value types such as Integers. When used on values, "Nothing" indicates the default value for the type. So an Integer assigned to Nothing is zero. Tip: Structures are too a value type.
When we assign them to Nothing, we get an empty Structure with no bits set to 1. A string in VB. NET can be assigned to Nothing, which is the same concept as Null. We can test a Nothing String with the String. IsNullOrEmpty method. NET program that uses String. Nothing is not just like null in C. Instead, it has a special behavior when used with value types—it acts like the default. That variable can then be used update the database.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to set Date variable to null in VB. NET Ask Question. Asked 9 years, 1 month ago. Active 10 months ago. Viewed 98k times. I want to assign the DOB variable as null. ToString "d" If chkSdob.
0コメント