registration
Are you an Employer?
Vb.net
List of Topics

Data Types

The .NET Common Language Runtime (CLR) includes the Common Type System (CTS), which defines the data types that are supported by the CLR. Thus, each of the languages in the .NET Framework (VB, C#, JScript, and Managed C++) implements a subset of a common set of data types. We say subset because, unfortunately, not all of the CTS types are implemented by VB .NET. For instance, the CTS includes some unsigned integer data types that are not implemented in VB.

As an aside, it is possible to use the VB-unsupported data types in VB by direct use of the corresponding Base Class Library class. Here is an example illustrating the ability to use the unsigned 16-bit integer data type, whose range of values is 0 to 65,535. Note the use of the ToUInt16 method of the Convert class to actually get an unsigned 16-bit integer:

ui = Convert.ToUInt16(65535)MsgBox(ui.ToString)

Thus, the native VB data types are wrappers for the CTS data types. To illustrate, the VB Integer datatype is a wrapper for the Int32 structure that is part of the .NET Framework's System namespace. One of the members of the Int32 structure is MaxValue, which returns the maximum value allowed for this data type. Thus, even though MaxValue is not officially part of VB .NET (nor is it mentioned in the VB documentation), we can write:

Dim i As IntegerMsgBox(i.Maxvalue) ' Displays 2147483647

Value and Reference Types The types defined in the CTS fall into three categories:

Value typesReference typesPointer types

However, pointer types are not implemented in VB, so we will not discuss these types. The difference between value and reference types is how variables of the corresponding type represent that type. When a value-type variable is defined, as in:

Dim int As Integer = 5

a memory location is set aside to hold the actual data (in this case the number 5). In contrast, when a reference-type variable is defined, as in:

Dim obj As New CEmployee

the VB compiler creates the object in memory, but then sets the variable obj to a 4-byte memory location that contains the address of the object.

In short, value-type variables contain the data, whereas reference-type variables point to the data. The distinction between value type and reference type has several consequences, one of which is in the way assignments work. To illustrate, consider the following class, which has a single property:

Public Class MyClassPublic Age As ShortEnd Class

and the structure MyStruct, also with a single property:

Structure MyStructPublic Age As ShortEnd Structure

Classes are reference types, whereas structures are value types. Now consider the following code, which is thoroughly commented:

Declare two class variables and two structure variables.Dim objRef1 As MyClassDim objRef2 As MyClassDim objValue1 As MyStructDim objValue2 As MyStruct' Instance the class.objRef1 = New MyClass( )' Set the Age property to 20.objRef1.Age = 20' Set the second variable to the first variable.' This is an equating of object *references* because' classes are reference types.objRef2 = objRef1' Set the Age property of objRef2 to 30.objRef2.Age = 30' Check the values of the Age property.Debug.WriteLine(objRef1.Age)Debug.WriteLine(objRef2.Age)' Do the same thing with the structure' Instance the structure.objValue1 = New MyStruct( )' Set the Age property to 20.objValue1.Age = 20' Set the second variable to the first variable.' This is an equating of object *values* because' structures are value types.objValue2 = objValue1' Set the Age property of objValue2 to 30.objValue2.Age = 30' Check the values of the Age property.Debug.Writeline(objValue1.Age)Debug.Writeline(objValue2.Age)
Now, the output is:
30302030

To understand what is happening, we need to realize that the reference assignment:

objRef2 = objRef1

sets both variables to the same value. But that value is the address of the object, and so both variables point to the same object. Hence, when we change the Age property using the second variable, this change is also reflected in the first variable.

On the other hand, the value assignment:

objValue2 = objValue1

causes a second structure to be created, setting the new structure's properties to the same value as the original structure. Thus, changing one structure's Age property does not affect the other structure's Age property.

Note that the VB Array type is also a reference type. To illustrate, consider the following code:

Dim iArray2( ) As IntegeriArray2 = iArray1iArray1(0) = 100msgbox(iArray2(0))

The message box displays 100, indicating that both array variables point to the same array.

The String data type is a reference type, implemented by the String class. However, it has some characteristics of a value type. To illustrate, consider the following code:

Dim s1, s2 As Strings1 = "String 1"s2 = s1s2 = "String 2"MsgBox(s1)

Since this is a reference type, we would expect the last line to produce the message "String 2", but instead we get "String 1". The reason can be found in Microsoft's documentation:

An instance of String is "immutable" because its value cannot be modified once it has been created. Methods that appear to modify a String actually return a new instance of String containing the modification.

Thus, the code: s2 = s1

points s2 to the same string as s1, as is usual with reference types. Then the attempt to modify the string in the code:

s2 = "String 2"

does not produce the expected result because strings are immutable. Instead, we get a new string pointed to by s2, while s1 retains its value.

The following code supports this conclusion:

Dim s1, s2 As Strings1 = "String 1"' s2 poitns to same string as s1s2 = s1' Show s2 before any changes to the stringMsgBox(s2) ' Displays "String1"' Change the strings2 = "String 2"' Set s1 to Nothings1 = Nothing' Now s1 is nothing and displays accordinglyMsgBox(s1) ' Displays nothing' s2 is a new stringMsgBox(s2) ' Displays "String 2"Enjoy!

VB Data Types: A Summary The following lists the data types supported by VB .NET, along with their underlying .NET type, storage requirements, and range of values:

Boolean

.NET CTS type: System.Boolean

Type: Value (Structure)

Storage: 2 bytes

Value range: True or False

Byte

.NET CTS type: System.Byte

Type: Value (Structure)

Storage: 1 byte

Value range: 0 to 255 (unsigned)

Char

.NET CTS type: System.Char

Type: Value (Structure)

Storage: 2 bytes

Value range: A character code from 0 to 65,535 (unsigned)

Date

.NET CTS type: System.DateTime

Type: Value (Structure)

Storage: 8 bytes

Value range: January 1, 1 CE to December 31, 9999

Decimal

.NET CTS type: System.Decimal

Type: Value (Structure)

Storage: 12 bytes

Value range: +/-79,228,162,514,264,337, 593,543,950,335 with no decimal point; +/- 7.9228162514264337593543950335 with 28 places to the right of the decimal; smallest nonzero number is +/-0.0000000000000000000000000001

Double (double-precision floating point)

.NET CTS type: System.Double

Type: Value (Structure)

Storage: 8 bytes

Value range: -1.79769313486231E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values

Integer

.NET CTS type: System.Int32

Type: Value (Structure)

Storage: 4 bytes

Value range: -2,147,483,648 to 2,147,483,647

Long (long integer)

.NET CTS type: System.Int64

Type: Value (Structure)

Storage: 8 bytes

Value range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Object

.NET CTS type: System.Object

Type: Reference (Class)

Storage: 4 bytes

Value range: Any type can be stored in an Object variable.

Short

.NET CTS type: System.Int16

Type: Value (Structure)

Storage: 2 bytes

Value range: -32,768 to 32,767

Single (single precision floating point)

.NET CTS type: System.Single

Type: Value (Structure)

Storage: 4 bytes

Value range: -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values

String (variable-length)

.NET CTS type: System.String

Type: Reference (Class)

Storage: 10 bytes + (2 * string length)

Value range: 0 to approximately 2 billion Unicode characters

User-Defined Type (structure)

.NET CTS type: (inherits from System.ValueType)

Type: Value (Structure)

Storage: Sum of the sizes of its members

Value range: Each structure member has range determined by its data type and is independent of the ranges of the other members.