This documentation is archived and is not being maintained.

Data Types Are .NET Framework Types 

Every elementary data type in Visual Basic is supported by a structure or a class in the System namespace. The compiler uses each data type keyword as an alias for the underlying structure or class. For example, declaring a variable with the reserved word Byte is the same as declaring it with the fully qualified structure name System.Byte.

Additional Classes and Structures Are Available in the .NET Framework

The common language runtime (CLR) also supports structures and classes that Visual Basic does not supply. For example, the System.Guid structure provides a globally unique identifier (GUID), and the System.TimeZone class supports a time zone. You can use these types to declare variables and constants, and you can access the methods the .NET Framework implements on these types. However, Visual Basic does not support operations or type conversions involving types it does not supply.

Value Types and Reference Types

In the .NET Framework, a structure is a value type and a class is a reference type. For this reason, value types such as Char and Integer are implemented by .NET Framework structures, while reference types such as Object and String are supported by .NET Framework classes. Note that every array is a reference type, even if its members are value types, and that every structure is a value type, even if it has reference type members.

Since every reference type represents an underlying .NET Framework class, you must use the New (Visual Basic) keyword when you initialize it. The following statement initializes an array.

Dim totals() As Single = New Single(8) {}

You can also use the New keyword to initialize a value type. This is particularly useful if the type has a constructor that takes parameters. An example of this is the Decimal constructor, which builds a new Decimal value from the supplied parts.

Data Types Have Members

Because they are supported by .NET Framework structures and classes, Visual Basic data types have members. These members include constructors, methods, properties, and fields. You can access the members (except the constructors) on a variable the same way you access methods and properties on an object.

The following example uses the Year, Month, and Day properties and the DaysInMonth method of the System.DateTime structure to determine how many days are remaining in the current month.

Dim current As Date = Now
Dim daysRemaining As Integer
daysRemaining = Date.DaysInMonth(current.Year, current.Month) - current.Day

Note that you must qualify a reference to a data type member with either the name of the type (Date) or the name of a variable declared to be of that type (current).

Examples of Data Type Members

The following code prototypes illustrate some of the useful methods, properties, and fields on the data types.

<Char>.IsDigit() ' Returns True if character is a numeric digit.

<Char>.IsLower() ' Returns True if character is a lowercase letter.

<Date>.IsLeapYear() ' Returns True if current year is a leap year.

<Date>.ToUniversalTime() ' Returns local date/time converted to UTC.

<Double>.IsInfinity() ' Returns True if contents represent infinity.

<Double>.IsNaN() ' Returns True if contents are not a number (0/0).

<Long>.MaxValue ' Constant representing largest positive Int64 value.

<Object>.GetType() ' Returns Type object representing type of <Object>.

<Object>.GetType().GetTypeCode() ' Returns type code of <Object>.

<String>.Chars(<index>) ' Character at position <index> of <String>.

<String>.Length ' Number of characters currently in <String>.

All numeric types, including Byte and Char, expose the MaxValue and MinValue public fields, which can be very useful when dealing with these types.

Equivalence of Data Type Members Is Not Assured

The .NET Framework also supplies a number of methods on data types that might appear to be equivalent to Visual Basic functions and keywords. However, Visual Basic does not always use the .NET Framework methods to accomplish conversion or other operations, and the results are not always identical.

For example, the ToSingle method performs the same type of action as the CSng keyword does on a Decimal expression. But CSng is not guaranteed to use System.<data type>.ToSingle, and therefore the results are not guaranteed to be the same under marginal or boundary conditions.

In general, you should use the Visual Basic programming elements because they are easier to use and make your code more readable. In some cases you might need some additional functionality provided by a .NET Framework method. For an example of this, see "Mod Operator Does Not Return Accurate Result" in Troubleshooting Data Types.

See Also