Chapter 7. The .NET Framework Class Library


With its move to .NET, Visual Basic is now about classes, classes, and more classes. Even something as simple as an Integer is implemented in a class (the System.Int32 class). As mentioned in Chapter 2, the .NET Framework defines an extensive network of classes and namespaces called the Framework Class Library (FCL). This library provides basic application development services, such as core data types, exception handling, and garbage collection, and support for higher-level functionality, such as database interaction, a forms and control package, and a web-based programming system. In total, there are about 200 namespaces containing several thousand classes, interfaces, structures, enumerations, and other items in the .NET Framework Class Library.

The term Base Class Library (BCL) refers to an important subset of the larger Framework Class Library. Because most programmers use the whole library without thinking much about whether they are using FCL or BCL, the terms are used interchangeably. You will find the terms sometimes used interchangeably even in this book.

The System namespace is at the top of the namespace hierarchy for most namespaces supplied with the .NET Framework, and the System.Object class is at the top of the object hierarchy. All types in the .NET Framework Class Library, no matter where they reside in the namespace hierarchy, derive from the System.Object class.

The .NET Framework Class Library is sufficiently extensive to require an entire book for itself. This chapter provides just a brief introduction and some examples. This should prepare you to dive into the Class Library documentation supplied with Visual Studio or available through Microsoft's MSDN web site (http://msdn.microsoft.com). In parallel with this chapter, you will find documentation for select library elements in Chapter 12, particularly those most useful to VB programmers.

Before becoming intimidated by the size of the Framework Class Library, keep in mind that VB provides wrappers for some elements of the FCL, so we can often just call a VB function rather than resort to accessing the classes in the larger library directly. More generally, while the class library does have much to offer a VB programmer and should not be ignored, it can be studied and used on an "as needed" basis.

New in 2005. Beginning with the 2005 release of Visual Basic, a larger number of library features are brought into easier use through the new My Namespace feature. This feature takes commonly used FCL activities and wraps them into a smaller, neatly organized hierarchy of tools. For full information on this new feature, see Chapter 13.

Here is a simple example of what the FCL has to offer beyond the basic Visual Basic language syntax. As discussed in Chapter 4, the built-in VB data types are wrappers for corresponding BCL data classes (for reference types) or structures (for value types). This means that your code has access to any special features included with each data type. If we want to verify that a user has entered an integer that lies within the valid range of the Integer data type, we can use code such as the following:

     Dim userEntry As String     Dim entryValue As Integer     userEntry = InputBox("Enter an integer.")     If IsNumeric(userEntry) Then        If (CDbl(userEntry) >= entryValue.MinValue) And _              (CDbl(userEntry) <= entryValue.MaxValue) Then           entryValue = CInt(userEntry)        Else           Debug.WriteLine("Invalid number.")        End If     Else        Debug.WriteLine("Non-numeric value.")     End If 

Visual Basic does not include features that indicate the lower and upper bounds of the Integer data type, but .NET's Int32 data type does. And since VB's Integer data type is simply a wrapper for the Int32 data type, VB gets all of its functionality for free, including the MinValue and MaxValue members. Incidentally, because MinValue and MaxValue are shared class members, the conditions in the sample code could also have been written as:

     If IsNumeric(userEntry) Then        If (CDbl(userEntry) >= Integer.MinValue) And _              (CDbl(userEntry) <= Integer.MaxValue) Then 




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net