perfectxml.com
Focus Info Bank Free Library Software News About Us
  You are here: home »» Free Library » Syngress Publishing Saturday, 23 February 2008

Sample Chapter from the book ASP.NET Web Developer's Guide. Reprinted by permission, Syngress Publishing © 2002.

Chapter 2

ASP.NET Namespaces

Solutions in this chapter:

·     Reviewing the Function of Namespaces

·     Using the Microsoft.VisualBasic Namespace

·     Understanding the Root Namespace: System

·     Grouping Objects and Data Types with the System.Collections Namespace

·     Enabling Client/Browser Communication with the System.Web Namespace

·     Working with Data Sources Using the System.Data Namespace

·     Processing XML Files Using the System.XML Namespace

·     Summary

·     Solutions Fast Track

·     Frequently Asked Questions

Introduction

Microsoft defines namespaces as "a logical naming scheme for grouping related types." What that means to us is that all objects used in ASP.NET are grouped by type, making them easy to find and to use. Imagine the .NET namespaces as a file cabinet. You use file cabinets to group related things to make finding them easier, and to preserve your sanity. For example, you may place the deed to your house and your mortgage coupons in one folder, while college loan papers and stubs go in another. Namespaces represent exactly the same concept. Like objects are grouped together: an HTMLInputTextBox object is grouped in the same namespace as the HTMLAnchor object, because they both represent HTML-user interface controls displayed to the user. In subsequent sections we'll be looking at all the major namespaces that ASP.NET will take advantage of.

System is the root of the namespaces. Within each namespace we can find anywhere from one to several other sub-namespaces that provide programmers with the functionality needed to create and provide Web-based applications.

System.Web is a great example. Within its namespace it contains over 10 different sub-namespaces that fulfill many of the basic Web functions and then some. System.Data contains various database connectivity methods, such as communication with SQL databases and some limited Extensible Markup Language (XML) connectivity. For specialized XML connectivity we can use System.XML, which can provide everything from parsing to translating XML schemas.

Reviewing the Function of Namespaces

As mentioned in the introduction, namespaces are logical collections of objects. You'll reference many namespaces and their objects throughout your ASP.NET development, so it's helpful to dig a bit deeper into the technology.

You should already have a grasp on the conceptual ideas behind namespaces—that they are containers for objects. However, how is this represented physically on your computer? A namespace is usually contained in a file called an assembly. These files look outwardly just like dynamically linked libraries (DLLs), and they even end in the .dll extension. If you are familiar with DLLs, then you'll know that prior to .NET, they were used to supply additional functionality and objects for your applications. In .NET, they do exactly the same thing, except that everything within the DLL file belongs to a specified namespace.

The main difference between .NET and non-.NET DLLs is that .NET DLLs are not compiled into machine language. Rather, they are compiled into the Microsoft Intermediate Language (MSIL), which is understood by the Common Language Runtime (CLR). Therefore, the two types of DLLs are not interchangeable (although you can build wrappers around non-.NET DLLs to make them compatible—see the .NET Framework Documentation under the tlbimp.exe tool).

Note that you can also create your own namespaces, or add to existing ones. See "Programming with Assemblies" in the .NET Framework Documentation for more information.

Using Namespaces

To use a namespace in an ASP.NET page, you must use the Import directive. For example, the following statement placed at the top of your ASP.NET page enables you to use the objects in the System.Data namespace:

<%@ Import Namespace="System.Data" %>

'more code

That's all you need to do. Behind the scenes, this instruction tells the CLR to reference this namespace when it compiles your ASP.NET application. The objects in the namespace then are dynamically loaded when they are called in your pages.

Migrating…

Compiling ASP.NET Pages

If you're familiar with classic ASP, the beginning of this section may have confused you. Classic ASP pages were not compiled—they were built with scripting languages (such as VBScript) and interpreted by the ASP.NET engine when they were called.

ASP.NET pages, however, are compiled before they are run. You build ASP.NET pages using a compiled language, such as VB.NET or C#. This serves to increase performance and strength tremendously over classic ASP.

Namespaces are a very powerful tool for developers. Because everything is grouped logically, you'll be able to find and infer an object's functionality much more easily than before. Often, just by knowing what namespace an object belongs to, you'll be able to use it without having to refer to documentation. Now let's take a look at the major namespaces available to ASP.NET.

Using the Microsoft.VisualBasic Namespace

The Microsoft.VisualBasic namespace, which is exclusive to Microsoft's Visual Basic, contains just one class, VBCodeProvider, and provides access to the Visual Basic.NET runtime, enabling you to interact with the compiler directly.

You won't be using this namespace often in your dealings with ASP.NET, unless you need to change the way ASP.NET pages are compiled (which is a very rare occurrence), so we'll move on. However, if you are interested in working more with VB.NET outside of ASP.NET, you should definitely explore this namespace further.

Understanding the Root Namespace: System

The System namespace is the root namespace for the entire .NET Framework; thus, it contains all the basic and generic classes you'll use in ASP.NET. These include the primitives (integers, strings, and so on), as well as all of the other namespaces in .NET. Since it is the root namespace, it is necessary to explore some of the major objects in this collection because they'll be used throughout all your future applications.

Supplied Functionality

Most of the functionality you'll be accessing from the System namespace involves the primitive data types, which the following sections will cover specifically. These include integral numbers, floating point numbers, date and time structures, string values, and Booleans, and additionally, the Object data type, which is generic. Table 2.1 describes the data types available.

Table 2.1 .NET Primitives

Primitive

Category

Description

Byte

Integers

1-byte integral number (System.Int)

Short

Integers

2-byte integral number (System.Int16)

Integer

Integers

4-byte integral number (System.Int32)

Long

Integers

8-byte integral number (System.Int64)

Single

Floating-points

4-byte number with decimal point (System.Single)

Double

Floating-points

8-byte number with decimal point (System.Double)

Decimal

Floating-points

12-byte number with decimal point (System.Decimal)

Char

Strings

A single Unicode character (System.Char)

Date

Dates

Date and/or time value (System.DateTime)

Boolean

Booleans

True or false value (System.Boolean)

Integral Numbers

Integral numbers are whole numbers that do not have decimal values. For instance: 1, 12353, and –10. If you are familiar with computer programming, you'll probably recognize the Byte, Short, Integer, and Long data types. These are 8, 16, 32, and 64 bit integers respectively, and each requires different amounts of memory. In other words, they can hold different ranges of values. For example, the Integer data type can hold values from -2,147,483,648 to 2,147,483,647.

You can reference these data types by the names in the preceding paragraph, or by the .NET names: System.Int, System.Int16, System.Int32, and System.Int64. Either name will work—the choice is up to you.

Floating-Point Numbers

Floating-point numbers are numbers with fractions or decimal points, such as 3.141592654 or –0.45. The specific data types are: Single (System.Single, 4 byte), Double (System.Double, 8 byte), and Decimal (System.Decimal, 12 byte). Let's take a look at a simple example. The following code illustrates the difference between integers and floating-point numbers.

1:  dim intA, intB as Integer

2:  dim fltA, fltB as Single

3: 

 

4:  intA = 4

5:  fltA = 5.6

6:  intB = intA * fltA

Line 6 should return the value 22.4, but since we've assigned it to intB, an Integer, the returned value is 22—ASP.NET has dropped the decimal point. The following line, however, will return the correct answer:

7:  fldB = intA * fltA

Be sure to use the proper data type for your applications!

Dates

A DateTime data type can be in many formats: "5/6/01," "Wednesday, July 4th, 2001," or "8:30:34 PM," for example. This provides you with great flexibility in representing your date values, and enables you to perform simple arithmetic (such as adding or subtracting days or hours) on your values. As you move through this book, you'll encounter many of these operations.

There is another date data type that you won't use as often, but is helpful to know: the TimeSpan data type, which represents a time interval such as "8 hours" or "13 days." Note that it cannot be used to hold specific times, such as "8 PM." Use the DateTime type for these values instead.

Strings

The String data type that most programmers are familiar with is actually a class in VB.NET, rather than a primitive. This enables you to create new instances, override, and inherit from a String, which gives the programmer a lot of power when designing applications. This is probably one of the most common classes you'll be using in your ASP.NET applications.

There is also the Char data type, which represents a single Unicode character. Because it is Unicode, it can represent a lot more than just the alphanumeric characters, in case you ever need to use them. You’ll see methods that will enable you to convert from Chars to Strings.

Booleans

Booleans are simply true-or-false values, such as 1/0, yes/no, and so on. Although the Boolean data type in VB.NET strictly uses true/false to represent data, you can easily convert it to the other pairs of values.

Objects

Finally, the Object data type is a generic type that's used for a variable if no other type is specified. For example, if you use the VB.NET statement, then you'll be creating an Object data type:

Dim strMyVariable

Note

It is generally a good practice to always explicitly declare your variable types. This saves you the trouble of having to convert later, as well as providing you with more functionality that can be used with your variables.

Your ASP.NET pages automatically import the System namespace, so you needn't import it explicitly. For example, the ASP.NET page shown in Figure 2.1 is equivalent to Figure 2.2—the latter is probably easier for the developer, and doesn't hurt performance at all.

Figure 2.1 Importing the System Namespace Explicitly

1:  <%@ Page Language="VB" %>

2:  <%@ Import Namespace="System" %>

3:  <script runat="server">

4:  dim MyInt as System.Integer

5:  </script>

Figure 2.2 Allowing ASP.NET to Implicitly Import the System Namespace

1:  <%@ Page Language="VB" %>

2:  <script runat="server">

3:  dim MyInt as Integer

4:  </script>

The System namespace also includes one more object that is very useful for ASP.NET developers: the Array. Even though this class belongs to the System namespace, we'll discuss it in the next section, under System.Collections.

Table 2.2 lists all of the namespaces directly under the System namespace—it's quite a long list, and each of these namespaces often have even more sub-namespaces. We'll cover a few of the more important ones (when dealing with ASP.NET) in the subsequent sections.

Table 2.2 The Namespace Collection

Namespaces

Description

CodeDom

Contains objects that represent the elements of a source code document.

Collections

Contains collection objects, such as lists, queues, and hash tables.

ComponentModel

Contains the classes that enable you to control the run and design-time behavior of components and controls.

Configuration

Provides methods and objects that enable you to access .NET configuration settings.

Data

Contains classes that enable you to interact with data sources; constitutes ADO.NET.

Diagnostics

Contains classes that enable you to debug and follow the execution of your applications.

DirectoryServices

Provides access to Active Directory services.

Drawing

Contains classes that enable you to use basic, graphical display interface (GDI) capabilities.

EnterpriseServices

Contains objects that enable you to control how components behave on a server.

Globalization

Contains classes that define culture-related information.

IO

Contains classes that enable you to read and write to data streams and files.

Management

Provides classes used to interface with WMI events and objects.

Messaging

Contains classes to interact with messages over a network.

Net

Provides classes to work with network protocols.

Reflection

Contains classes that enable you to view information about other types in the .NET Framework.

Resources

Contains classes that enable you to manage culture-specific resources.

Security

Provides access to the .NET security framework.

ServiceProcess

Enables you to interact with services.

Text

Contains classes that represent ASCII, Unicode, UTF-7, and UTF-8 character encodings.

Threading

Contains classes that enable multi-threaded programming.

Timers

Contains classes to raise events on specified time intervals.

Web

Provides client/browser communications; represent the bulk of objects that will be used with ASP.NET.

Xml

Contains classes that process XML data.

Grouping Objects and Data Types with the System.Collections Namespace

The System.Collections namespace contains much of the functionality you'll need for grouping objects and data types into collections. These include lists, arrays, hash tables, and dictionaries, as well as some collections that you won't see as often in ASP.NET: stacks, comparers, and queues.

Supplied Functionality

The classes in the System.Collections namespace are often very useful, but unfortunately are often not in the spotlight in ASP.NET. They each have specific uses that just may come in handy for your applications. They are listed in Table 2.3.

Table 2.3 The System.Collections Classes

Name

Description

ArrayList

Creates an array whose size is dynamically increased as necessary.

BitArray

Provides an array of bits (Boolean values).

CaseInsensitiveComparer

Provides case-insensitive comparison of two objects.

CaseInsensitiveHashCodeProvider

Creates hash codes for objects, ignoring cases for strings.

CollectionBase

The base class for a strongly typed collection. This class must be inherited fromit cannot be directly instantiated.

Comparer

A case-sensitive object comparison class.

DictionaryBase

The base class for a strongly typed collection of key/value pairs. This class must also be inherited from.

Hashtable

A collection of key/value pairs organized by the hash value of the key.

Queue

A first-in, first-out collection of objects.

ReadOnlyCollectionBase

Just like the CollectionBase class, but the values are read-only.

SortedList

A collection of key/value pairs sorted by the key value.

Stack

A last-in, first-out collection of objects.

In addition to the classes outlined in Table 2.3, there is the System.Array class, which holds collections of values. Let's take a look at an example. The following code creates an array of integers, initialized to the numbers 1 to 5:

Dim arrIntegers() As Integer = {1, 2, 3, 4, 5}

The size of this array is 5, and the index values are 0 to 4. For example, to access the number 3 in this array, you would use this:

arrIntegers(2)

Note that you cannot declare a size for an array and assign values at the same time. The following code would produce an error:

Dim arrIntegers(5) As Integer = {1, 2, 3, 4, 5}

Instead, separate the declaration and assignation into two steps:

Dim arrIntegers(5)

arrIntegers(0) = 1

arrIntegers(1) = 2

'and so on

 

The Array class has quite a few useful methods and properties as well, such as the Copy and Sort methods, and the Length and Rank properties. You'll examine these more as you progress through the book.

Goto Page 2 >>
  Contact Us | E-mail Us | Site Guide | About PerfectXML | Advertise ©2004 perfectxml.com. All rights reserved. | Privacy