Value Type, reference type, and nullable type in VB. NET

Source: Internet
Author: User

 

There are three data types in VB. NET: Value Type, reference type, and nullable type. This article starts with the condition judgment and discusses the differences between the three.

Value Type

Classes inherited from system. valuetype are generally called value types, including dates and numbers. The default value of a date object is "0001-01-01 00:00:00", and that of a number object is "0 ". There are two ways to specify the default value type: no value or set to nothing. For example:
Dim dt1 as date <br/> dim dt2 as date = nothing
The values of the preceding two variables are "0001-01-01 00:00:00 ". Setting nothing to a value type variable is to specify the default value, which is indeed a bit difficult to understand, but indeed. For example:
Dim TMP as integer = nothing <br/> console. writeline (TMP) '=> 0
You can only use "=" to determine whether the value type variables are equal ". For example:
Dim TMP as integer = 1 <br/> console. writeline (TMP = 1) '=> true

Reference Type

The value type corresponds to the reference type, and the character or string type is the reference type. Variables of the reference type Save the object address. When such variables are set to nothing, they point to an empty address.
Dim TMP as string = nothing <br/> console. writeline (TMP = nothing) '=> true
In addition to using "=" for judgment, variables of the reference type can also be judged by "is. "=" Is used to compare whether the content of two objects is the same; "is" is used to compare whether two references point to the same object. The following example proves this:
Dim str1 as string = "tmp" <br/> dim str2 as string = "tmp" <br/> dim str3 as string = new string (ctype ("tmp ", char () <br/> console. writeline (str1 = str2) '=> true: the content of the two objects is the same <br/> console. writeline (str1 is str2) '=> true two references point to the same object <br/> console. writeline (str1 = str3) '=> true: the content of the two objects is the same <br/> console. writeline (str1 is str3) '=> false two references point to different objects

Nullable type

To make a variable of the value type have the feature of the reference type, we can use the nullable type. For example:
<textarea cols="50" rows="15" name="code" class="vb">Dim TMP as nullable (of integer) = nothing <br/> console. writeline (TMP is nothing) '=> true</textarea>
The nullable type is a type between the value type and the reference type. We can use "is" to determine whether the nullable variable is nothing, however, "is" cannot be used to determine whether two nullable variables reference the same object. On the other hand, we can use "=" to determine whether the content of the two objects is the same, however, in some cases, the nullable variable does not return the familiar true or false values. For example:
<textarea cols="50" rows="15" name="code" class="vb">Dim TMP as nullable (of integer) = nothing <br/> If TMP <> 1 then <br/> '<SPAN class = 'wp _ keywordlink'> Code </span> not found here <br/> Console. writeline ("tmp <> 1") <br/> end if <br/> If TMP = 1 then <br/> 'the code is not here. <br/> console. writeline ("tmp = 1") <br/> end if</textarea>
By checking the memory, you can know that in the above example (TMP = 1) or (TMP <> 1), all returned nothing, instead of true or false. This phenomenon will directly lead to misjudgment. How can we avoid this phenomenon?
Use the static method provided by the nullable class to compare nullable variables.
The above code can be modified as follows:
<textarea cols="50" rows="15" name="code" class="vb">Dim TMP as nullable (of integer) = nothing <br/> if not nullable. equals (TMP, 1) Then <br/> 'the code is here <br/> console. writeline ("tmp <> 1") <br/> end if <br/> If nullable. equals (TMP, 1) Then <br/> 'the code is not here <br/> console. writeline ("tmp = 1") <br/> end if</textarea>
All in all, the three data types of VB. NET have their own usage. Pay special attention to the usage of the nullable type.

 

Ugly Duckling technical Column

| View Original Text

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.