ONDotNet.com    
 Published on ONDotNet.com (http://www.ondotnet.com/)
 See this if you're having trouble printing code examples


O'Reilly Book Excerpts: VB.NET Core Classes in a Nutshell

An Introduction to the .NET FCL, Part 5

Related Reading

VB.NET Core Classes in a Nutshell
By Budi Kurniawan, Ted Neward

by Budi Kurniawan and Ted Neward

In this final installment from VB.NET Core Classes in a Nutshell, learn how to approach the .NET FCL.

Approaching the .NET FCL

It may seem that, given both the newness and the enormity of the .NET platform, a substantial learning curve is required to "learn" the .NET FCL. In fact, this isn't the case; you can begin to take advantage of the class library immediately by selecting those classes, structures, and enumerations and their members that are of immediate interest to you and ignoring the rest. You can then gradually expand your familiarity with the .NET FCL as needed.

This incremental approach to learning the .NET FCL is possible because Visual Basic was written to run under the .NET platform, and much of the Visual Basic language (or at least the functions and procedures not implemented directly by the Visual Basic compiler) actually wrap functionality found in the .NET FCL.

The clearest example of this is to be found in the data types supported by Visual Basic. While Visual Basic's data types appear to be intrinsic, in fact they are defined by the .NET FCL; Visual Basic merely provides wrappers for each of the .NET data types for which it offers native support. This, in fact, is one of the major strengths of the .NET Framework: it features the Common Type System (CTS), which allows components and applications written in one .NET-compliant language to more or less seamlessly interoperate with components written in other .NET-compliant languages. Table 1-1 shows the "intrinsic" Visual Basic data types and their corresponding .NET FCL data types.

Table 1-1. VB.NET data types and their corresponding .NET FCL data types

VB.NET data type

.NET FCL data type

Boolean

System.Boolean

Byte

System.Byte

Char

System.Char

Date

System.DateTime

Decimal

System.Decimal

Double

System.Double

Integer

System.Int32

Long

System.Int64

Object

System.Object

Short

System.Int16

Single

System.Single

String

System.String

That the standard VB data types are merely wrappers for CTS data types is indicated by the Visual Basic SystemTypeName function, which returns the name of a CTS data type that corresponds to a particular "intrinsic" VB data type. For example, the code fragment:

Dim i as Integer = 12345
Dim s As String = "New World"
  
Console.WriteLine(SystemTypeName(TypeName(i)))
Console.WriteLine(SystemTypeName(TypeName(s)))

shows that the VB Integer corresponds to the .NET System.Int32 data type, while the VB String data type corresponds to the .NET System.String data type. In other words, we could also declare and initialize our two variables as follows:

Dim i as System.Int32 = 12345
Dim s As System.String = "New World"

The fact that VB data types are really CTS data types means that we can access the fields, properties, and methods of CTS datatypes from VB variables. Consider, for example, the following code fragment:

Dim d As Double
Dim b As Byte
Dim s As String = InputBox("Enter a number (0-255): ")
  
If IsNumeric(s) Then
   d = CDbl(s)
   If b.MaxValue >= d And b.MinValue <= d Then
      b = CByte(s)
   End If
   Console.WriteLine(TypeName(b) & " value: " & b)
End If

The code simply checks whether the numeric equivalent of a string entered by the user is within the range of the VB Byte data type by retrieving the values of the System.Byte data type's MinValue and MaxValue fields.

TIP: Because they don't exist in the Framework Class Library, two intrinsic data types found in previous versions of VB have been removed from the language. The first is the Currency data type. In its place, use the Decimal data type, which in the .NET platform is a standard data type. (In previous versions of VB, the Decimal was a subtype of the Variant data type, and variables could be cast as decimals only by calling the CDec conversion function.) The second is the Variant data type, which has been replaced by the Object data type as VB's "universal" data type.

Moreover, the reverse is also true: given a CTS data type, we can pass it as a parameter to methods that work on Visual Basic data types. For example:

Option Strict On
Imports Microsoft.VisualBasic
Imports System
  
Public Module modMain
  
Public Sub Main
   Dim iNum As Int32 = 1234
   Dim sNum As System.String = CStr(iNum)
  
   Console.WriteLine(Mid(sNum,3,2))
End Sub
  
End Module 

This code includes two instances of calls to VB.NET methods using CTS data types. The first is the call to the CStr conversion function, which is passed a variable of type Int32. The second is the call to the Mid string manipulation function, which is passed a variable of type System.String.

This means that, when working with Visual Basic data types, you can continue to call intrinsic Visual Basic functions, and call the members of .NET data types when they provide important functionality not available directly from Visual Basic. The following sections detail some of those functions.

Array Class

Just as Visual Basic scalar data types are in fact CTS scalar data types, so Visual Basic arrays are actually members of the .NET System.Array class. Some of its members that are not readily available in Visual Basic .NET are shown in the following table:

Name

Member type

Description

BinarySearch

Shared method

Searches a one-dimensional array for a particular value

Clear

Shared method

Sets a range of array elements to zero, False, or a null reference, depending on the members' data type

Copy

Shared method

Copies a portion of one array to another and performs any necessary type conversions

CopyTo

Method

Copies all the elements of a one-dimensional array to another one-dimensional array starting at a particular index position

IndexOf

Shared method

Returns the index of the first occurrence of a particular value in an array

IsFixedSize

Property

Returns a Boolean indicating whether an Array object has a fixed size

IsReadOnly

Property

Returns a Boolean indicating whether the elements in Array object are read-only

LastIndexOf

Shared method

Returns the index of the last occurrence of a particular value in an array

Rank

Property

Returns an Integer indicating the number of dimensions of the array

Reverse

Shared method

Reverses the elements in all or part of a one-dimensional array

Sort

Shared method

Sorts a one-dimensional array


Boolean Structure

The following table lists the members of the System.Boolean structure that are not readily available in the Visual Basic .NET language:

Name

Member type

Description

FalseString

Shared field

Returns the string representation of the Boolean value False. This is a read-only constant value.

Parse

Shared method

Converts a string (whose value must be either Boolean.TrueString or Boolean.FalseString) to its Boolean equivalent.

TrueString

Shared field

Returns the string representation of the Boolean value True. This is a read-only constant value.


Byte Structure

The Visual Basic .NET Byte data type is synonymous with the .NET System.Byte data type. The following table lists some of the members of the Byte class that are not readily available in Visual Basic:

Name

Member type

Description

MaxValue

Shared field

A constant representing the largest possible value of an instance of the Byte class

MinValue

Shared field

A constant representing the smallest possible value of an instance of the Byte class

Parse

Shared method

Converts the numeric representation of a string to its Byte equivalent


Char Structure

Char, a new data type in VB.NET, corresponds to the System.Char data type. Some of the members of the latter that offer functionality not available in VB.NET are shown in the following table:

Name

Member type

Description

GetUnicodeCategory

Shared method

Returns a member of the UnicodeCategory enumeration (in the System.Globalization namespace) indicating the character type of a Char object.

IsControl

Shared method

Returns a Boolean indicating whether a particular Unicode character is a control character.

IsDigit

Shared method

Returns a Boolean indicating whether a particular Unicode character is a decimal digit. (The decimal and thousands separators are not considered digits.)

IsLetter

Shared method

Returns a Boolean indicating whether a particular Unicode character is a letter of the alphabet.

IsLetterOrDigit

Shared method

Returns a Boolean indicating whether a particular Unicode character is a digit or a letter of the alphabet.

IsLower

Shared method

Returns a Boolean indicating whether a particular Unicode character is a lowercase letter. The method returns True only for letters of the alphabet; that is, Char.IsLetter must also return True.

IsNumber

Shared method

Returns a Boolean indicating whether a particular Unicode character is a decimal or hexadecimal digit.

IsPunctuation

Shared method

Returns a Boolean indicating whether a particular Unicode character is a punctuation mark.

IsSeparator

Shared method

Returns a Boolean indicating whether a particular Unicode character is a separator character (e.g., a space). The method does not return True if the character is a numeric separator.

IsSymbol

Shared method

Returns a Boolean indicating whether a particular Unicode character is a symbol.

IsUpper

Shared method

Returns a Boolean indicating whether a particular Unicode character is an uppercase letter. The method returns True only for letters of the alphabet; that is, Char.IsLetter must also return True.

IsWhiteSpace

Shared method

Returns a Boolean indicating whether a particular Unicode character is white space.

MaxValue

Shared field

A constant containing the largest possible value of Char object.

MinValue

Shared field

A constant containing the smallest possible value of a Char object.

Parse

Shared method

Converts a particular character in a string to a Char object.


DateTime Structure

The Visual Basic Date data type corresponds to the .NET Framework's DateTime structure. The following table lists the DateTime members whose functionality is not available in the date/time functions supported by the VB.NET language:

Name

Member type

Description

AddMilliseconds

Method

Adds a designated number of milliseconds to the DateTime instance

AddTicks

Method

Adds a designated number of ticks to the DateTime instance

CompareTo

Method

Compares the current DateTime instance to an object and returns an indication of their relative values

DaysInMonth

Shared method

Returns the number of days in a designated month and year

FromOADate

Shared method

Converts an OLE Automation Date value to a DateTime instance

GetDateTimeFormats

Method

Returns a String array containing all the string representations supported by the standard DateTime format specifiers

IsLeapYear

Shared method

Returns a Boolean indicating whether a particular year is a leap year

MaxValue

Shared field

A constant containing the largest possible value of a DateTime instance

Millisecond

Property

Retrieves the milliseconds component of a DateTime instance

MinValue

Shared field

A constant containing the smallest possible value of a DateTime instance

Now

Shared property

Returns the current local date and time

Parse

Shared method

Converts a string representation of a date/time to a DateTime instance

ParseExact

Shared method

Converts a string representation of a date/time in a specified format to its DateTime equivalent

Ticks

Property

Returns a Long containing the number of ticks that represent the date and time value of this instance

TimeOfDay

Property

Returns the current time of day

Today

Shared property

Returns the current date

ToFileTime

Method

Converts the DateTime instance to the format of the local system's file time

ToLocalTime

Method

Converts the current coordinated universal time (UTC) to local time

ToLongDateString

Method

Converts a DateTime instance to its long date string representation

ToLongTimeString

Method

Converts a DateTime instance to its long time string representation

ToOADate

Method

Converts the value of this instance to a Double representing the OLE Automation date

ToShortDateString

Method

Converts a DateTime instance to its short date string representation

ToShortTimeString

Method

Converts a DateTime instance to its short time string representation

ToUniversalTime

Method

Converts the value of the DateTime instance to coordinated universal time (UTC)

UtcNow

Shared property

Returns a DateTime instance that represents the current local date and time expressed as the coordinated universal time (UTC)


Decimal Structure

The VB.NET Decimal data type corresponds directly to the System.Decimal structure. The CTS Decimal structure includes the following members whose functionality is not readily available in VB.NET:

Name

Member type

Description

FromOACurrency

Shared method

Converts a Long containing an OLE Automation Currency value (e.g., a VB6 or VBScript Currency value) to a Decimal value

GetBits

Shared method

Converts a particular Decimal value to its binary representation and returns that value as an Integer

MaxValue

Shared field

A constant containing the largest possible value of a Decimal object

MinValue

Shared field

A constant containing the smallest possible value of a Decimal object

ToOACurrency

Shared method

Converts a Decimal value to an OLE Automation Currency value (e.g., a VB6 or VBScript Currency value), which it returns as a Long


Double Structure

The VB.NET Double data type corresponds to the .NET Framework's System.Double data type. The following table lists the members of the latter that offer functionality not found in the Visual Basic .NET language:

Name

Member type

Description

Epsilon

Shared field

A constant containing the smallest positive Double value greater than zero

MaxValue

Shared field

A constant containing the largest possible value of a Double object

MinValue

Shared field

A constant containing the smallest possible value of a Double object

NaN

Shared field

A constant containing the representation of a value that is not a number (NaN)

NegativeInfinity

Shared field

A constant containing a number that represents negative infinity

PositiveInfinity

Shared field

A constant containing a number that represents positive infinity

IsInfinity

Shared method

Returns a Boolean indicating whether the value of a Double object represents positive or negative infinity

InNaN

Shared method

Returns a Boolean indicating whether a Double object contains a value that is not a number (NaN)

IsNegativeInfinity

Shared method

Returns a Boolean indicating whether the value of a Double object represents negative infinity

IsPositiveInfinity

Shared method

Returns a Boolean indicating whether the value of a Double object represents positive infinity

Parse

Shared method

Converts the string representation of a number to its Double equivalent

Int16 Structure

The VB.NET Short data type corresponds directly to the System.Int16 structure. The members of the latter structure that offer unique functionality are shown in the following table:

Name

Member type

Description

MaxValue

Shared field

A constant containing the largest possible value of an Int16 object

MinValue

Shared field

A constant containing the smallest possible value of an Int16 object

Parse

Shared method

Converts the string representation of a number to its Int16 equivalent


Int32 Structure

The VB.NET Integer data type corresponds directly to the System.Int32 structure. The members of the Int32 structure that offer unique functionality are shown in the following table:

Name

Member type

Description

MaxValue

Shared field

A constant containing the largest possible value of an Int32 object

MinValue

Shared field

A constant containing the smallest possible value of an Int32 object

Parse

Shared method

Converts the string representation of a number to its Int32 equivalent


Int64 Structure

The VB.NET Long data type corresponds directly to the System.Int64 structure. The members of the Int64 structure that offer unique functionality are shown in the following table:

Name

Member type

Description

MaxValue

Shared field

A constant containing the largest possible value of an Int64 object

MinValue

Shared field

A constant containing the smallest possible value of an Int64 object

Parse

Shared method

Converts the string representation of a number to its Int64 equivalent


Object Class

The VB.NET Object data type corresponds to the System.Object class. System.Object, however, offers no functionality that is not available through the standard Visual Basic language.

Single Structure

The VB.NET Single data type corresponds to the .NET Framework's System.Single data type. The following table lists the members of the latter that offer functionality not found in the Visual Basic .NET language:

Name

Member type

Description

Epsilon

Shared field

A constant containing the smallest positive Single value greater than zero

MaxValue

Shared field

A constant containing the largest possible value of a Single object

MinValue

Shared field

A constant containing the smallest possible value of a Single object

NaN

Shared field

A constant containing the representation of a value that is not a number (NaN)

NegativeInfinity

Shared field

A constant containing a number that represents negative infinity

PositiveInfinity

Shared field

A constant containing a number that represents positive infinity

IsInfinity

Shared method

Returns a Boolean indicating whether the value of a Single object represents positive or negative infinity

InNaN

Shared method

Returns a Boolean indicating whether a Single object contains a value that is not a number (NaN)

IsNegativeInfinity

Shared method

Returns a Boolean indicating whether the value of a Single object represents negative infinity

IsPositiveInfinity

Shared method

Returns a Boolean indicating whether the value of a Single object represents positive infinity

Parse

Shared method

Converts the string representation of a number to its Single equivalent


String Class

The VB.NET String data type is equivalent to the System.String class. The members of the String class that offer functionality not incorporated in the VB.NET language are shown in the following table:

Name

Member type

Description

Chars

Property

Returns a Char instance representing the character at a particular position in a string

Clone

Method

Returns an additional reference to a particular String instance

CompareOrdinal

Shared method

Compares two strings without considering locale settings

CopyTo

Method

Copies a designated number of characters starting at a particular position in a string to a character array

Empty

Shared field

A constant representing an empty string

EndsWith

Method

Returns a Boolean indicating whether the String instance ends with the substring passed to the method as an argument

Format

Shared method

Replaces each format specification in a string with its corresponding value

IndexOf

Method

Returns the position of the first occurrence of a character or a substring within the string instance

IndexOfAny

Method

Returns the index of the first occurrence in this String instance of any element in a character array

Insert

Method

Inserts a substring at a designated position of a String instance

LastIndexOf

Method

Returns the position of the last occurrence of a designated character or substring within the string instance

LastIndexOfAny

Method

Returns the position of the last occurrence in this String instance of any of a set of characters in a character array

PadLeft

Method

Right aligns the characters in a String instance by padding them with a space or another designated character

PadRight

Method

Left aligns the characters in a String instance by padding them with a space or another designated character

Remove

Method

Deletes a specified number of characters from a String instance beginning at a designated position

StartsWith

Method

Returns a Boolean indicating whether the String instance begins with a designated substring or character

ToCharArray

Method

Copies the characters in the String instance to a Unicode character array


Non-CTS Data Types

Note, however, that because they are not part of the Common Language Specification (the specification that defines the core functionality that a .NET platform must implement), VB does not wrap the following CTS data types:

System.SByte

Description: Signed byte

Value Range: -128 to 127

System.UInt16

Description: Unsigned 16-bit integer

Value Range: 0 to 65,535

System.UInt32

Description: Unsigned 32-bit integer

Value Range: 0 to 4,294,967,295

System.UInt64

Description: Unsigned 64-bit integer

Value Range: 0 to 18,446,744,073,709,551,615

Budi Kurniawan is a senior J2EE architect and author.

Ted Neward is an independent software development architect and mentor in the Sacramento, California area.


VB.NET Core Classes in a  Nutshell

Related Reading

VB.NET Core Classes in a Nutshell
By Budi Kurniawan, Ted Neward

Return to .NET DevCenter.

Copyright © 2009 O'Reilly Media, Inc.