This documentation is archived and is not being maintained.

Data Types as Classes and Structures

Every elementary data type in Visual Basic .NET 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.

The common language runtime also supports structures and classes that Visual Basic .NET does not supply. For example, the System.UInt16, System.UInt32, and System.UInt64 structures provide the unsigned equivalents of Short, Integer, and Long. You can use these unsigned types to declare variables and constants, and you can access the methods .NET implements on these types. However, Visual Basic .NET does not support operations or type conversions involving types it does not supply.

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 supported by .NET structures, while reference types such as Object and String are supported by .NET classes.

Since every reference type represents an underlying .NET class, you must use the New 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.

Data Type Members

Because they are supported by .NET 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   ' Date data type is supported by System.DateTime.
Dim DaysRemaining As Integer   ' Days remaining in current month.
' ...
Current = Now   ' Get current date/time into Current.
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).

Data Type Member Examples

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.

Data Type Member Equivalence

The .NET Framework also supplies a number of methods on data types that might appear to be equivalent to Visual Basic functions and keywords. For example, System.Decimal.ToSingle() performs the same type of action as the CSng keyword does on a Decimal expression. However, Visual Basic does not always use the .NET Framework methods to accomplish conversion or other operations. 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.

See Also

Value Types and Reference Types | Structures and Classes | System Namespace | Boolean Structure | Byte Structure | Char Structure | DateTime Structure | Decimal Structure | Double Structure | Int16 Structure | Int32 Structure | Int64 Structure | Object Class | Single Structure | String Class