WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button 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.

Pages: 1, 2, 3, 4

Next Pagearrow