By Budi Kurniawan, Ted Neward
Price: $44.95 USD
£31.95 GBP
Cover | Table of Contents | Colophon
' Single statement
Dim oFile As New FileInfo("c:\documents\notes.txt")
' Single Statement with separate call to constructor
Dim oFile As FileInfo = New FileInfo("c:\documents\notes.txt")
' Separate declaration and initialization
Dim oFile As FileInfo
oFile = New FileInfo("c:\documents\notes.txt")
WithEvents keyword.
For example:Dim WithEvents cn As New SqlConnection()
|
VB.NET
data type
|
.NET
FCL data type
|
|---|---|
|
Boolean
|
System.Boolean
|
|
Byte
|
System.Byte |
MustInherit are shown as a slanted
rectangle, and classes marked as NonInheritable are shown as an octagonal
rectangle. Inheritance is shown as a solid line from the subtype, ending with
a hollow triangle that points to the base class. There are two notations that
indicate interface implementation. The lollipop notation is used most of the
time, since it is easier to read. In some cases, especially where many types
implement a given interface, the shaded box notation with the dashed line
is used. MustInherit are shown as a slanted
rectangle, and classes marked as NonInheritable are shown as an octagonal
rectangle. Inheritance is shown as a solid line from the subtype, ending with
a hollow triangle that points to the base class. There are two notations that
indicate interface implementation. The lollipop notation is used most of the
time, since it is easier to read. In some cases, especially where many types
implement a given interface, the shaded box notation with the dashed line
is used. NonInheritable or MustInherit.Microsoft.Win32 namespace includes types you can use
to interact with the Microsoft Windows platform. These types concentrate on
two aspects of Windows-specific programming: receiving operating system
events and manipulating the registry. Essentially, the classes, delegates,
and enumerations in this namespace provide a type-safe object wrapper
around a few select functions in the Windows API.
Registry class to access
the root level registry keys, which are provided as RegistryKey objects. These have built-in
methods for retrieving and modifying key values. The SystemEvents class allows you to respond
to system events such as timers, preference changes, and shutdown operations.
All other types in this namespace are used to support the SystemEvents class, by providing
delegates for each event and custom System.EventArgs
objects that provide additional information to the
corresponding event handlers. Figure 3-1 shows
the inheritance diagram for this namespace.
System.EventArgs object
for the PowerModeChangedEventHandler
delegate. It provides additional information to your event handler, identifying
the new power mode that the system has entered.
System.Object→
System.EventArgs→
PowerModeChangedEventArgs
PowerModeChangedEventHandler.{BeginInvoke()System namespace serves as the
core namespace for the .NET libraries, in much the same
way java.lang does for Java programmers or stdlib.h does for C/C++ programmers. For example, the
ECMA-compliant primitive-type value types are defined in the System namespace, along with
complementary composite types and base types. These are used in the synthesis of
type generation, which is done by the compiler on the .NET programmer's behalf
(for an example of this on-the-fly type synthesis, see Array).
Figure 4-1 shows many of the types in this namespace.
System serves as the home for key base-type definitions,
including Object, the root of every type in the
.NET hierarchy. Every type in the system ultimately extends this class,
making it the "root of all evil" in .NET. In addition, this
namespace contains ValueType, the base type for
all value types in .NET (such as the primitive types listed later in this chapter,
shown in Figure 4-5), and Type, which in turn represents
compile-time type information about other types defined within the .NET
environment (the type metadata).
Int32, Single, Double, Decimal, Char, Byte, and Boolean.
All of the primitive types are aliased in VB.NET with keywords such
as Integer, Double, and Boolean.
See the description of each type for more
details.
In addition to these fundamental types, there are
composite types such as DateTime and TimeSpan, used to handle date- and time-based
calculations without having to drop down to integer math, and Uri, used to represent references to a
Universal Resource Identifier, which is the more generic form of the ubiquitous
HTTP URL identifier used on the Web.
System.Collections namespace provides basic functionality
for collections of objects. It defines
interfaces, base classes, and implementations for
collections such as dictionaries, sorted lists,
queues, and stacks. The base classes can also be extended
to create specialized collection types.
However, the System.Collections.Specialized namespace contains a set of extended collection
types based on this namespace, so check there before creating your own types.
Figure 5-1 and Figure 5-2 show the types in this namespace.
IList interface and
the ArrayList implementation? On top of
this, the namespace defines a number of other interfaces, such as IEnumerable and IEnumerator, that
seem unnecessary.
For Each
construct works by silently using the IEnumerable
interface to obtain an object that inherits the IEnumerator interface. Thus, a programmer
could, if desired, create a custom type (perhaps modeling a hand of cards) that
acts just as any other collection class does. Alternatively, the iterator (the
type that inherits from IEnumerator) could
be a "smart" iterator, knowing how to walk through (or skip past) types in the
container itself. All this is possible solely because the interface is
separated from the implementation; it is decoupled.
System.Collections
namespace are fine
as general-purpose collection types, but frequently programmers require
specialized semantics around a collection class; for example, storing a collection
of booleans could be more efficiently stored as a single System.Int64,
whereas simply placing System.Boolean instances into a general-purpose
collection is far more wasteful, in both memory and processing time.
System.Collections.IDictionary
interface. Note that it clearly defines a mapping of keys to values;
however, it is only implicitly understood that the exact same
key must be produced to obtain
the value desired. In most cases, this is exactly what's needed; however, there
are times when a less stringent retrieval mechanism is preferred. For example,
perhaps a case-insensitive match
is wanted instead of doing an exact-match for a string key. The System.Collections.Specialized namespace includes
collections designed to address these cases.
Figure 6-1 shows the types in this
namespace.
Process type. Use this type to launch new
processes or take control of processes currently running on the
system. The ProcessThread type lets you drill
down into each thread that's running within a process for
fine-grained control over running applications.
System.Globalization namespace provides classes
that assist in localization of applications based on language and
culture. The CultureInfo class is the primary container for
a set of resources that is used for a specified language and culture
implementation. It describes how strings are sorted, the specifics
of calendars and date and time formats, as well as language and
dialect code pages. An application obtains its culture information
based on either the CultureInfo specified by the current thread or
from the user or local machine's preferences. Specific cultural
information is contained in resource files deployed in satellite assemblies. System.Resources.ResourceManager marshals these resource files
into System.Resources.ResourceSets that provide
the objects and methods specific to a localization.
System.Globalization namespace provides
a base Calendar class, as well
as specific calendar implementations for major cultures. CompareInfo
defines how string comparison and sorting are handled. DateTimeFormatInfo
defines how DateTime values are formatted, and NumberFormatInfo defines various formatting
styles, such as currency symbols and decimal and grouping separators.
Figure 8-1 shows the types in this namespace.
DateTimeFormatInfo to properly display a
date and time string from a specific System.IO types serve as the primary means for
stream-oriented I/O — files, principally, although the MustInherit types
defined here serve as base classes for other forms of I/O, such as the XML
stack in System.Xml. The System.IO
namespace is shown in Figure 9-1 and Figure 9-2.
System.IO namespace can be seen as two
distinct partitions: a set of utility types for using and working with the
local machine's filesystem, and a protocol stack for working with bytestream-oriented
input and output. The former partition is the collection of classes such as Directory and FileSystemWatcher,
whereas the latter partition is the set of Stream
and Reader/Writer types.
Stream types in System.IO follow a basic object
model, similar to the I/O model used by the
C/C++ runtime library: all serial byte access is a stream, and
there are different sources and sinks for this serialized byte
data. In the System.IO package, this is represented
directly by the MustInherit base type Stream; its
concrete subtypes represent the actual I/O access: FileStream
represents I/O to a file, and MemoryStream represents I/O to
a literal array of bytes (whose size is dynamically managed) in memory.
Other packages within the .NET Framework Class Library offer up their own Stream-derived types. For example, in the System.Net
namespace, socket connections and HTTP responses are offered up as Stream-derived types, giving .NET programmers the ability to treat any
sort of input or output data as "just a Stream."
System.IO.IsolatedStorage allows you to access an
isolated area of a filesystem for your application. This is useful
when access to the System.IO classes is not
possible. The security settings of the .NET Framework prohibit web
applications and downloaded controls from accessing the local
filesystem directly, but those settings allow them to use System.IO.IsolatedStorage.
Applications' storage areas are isolated from one another, so
anything in isolated storage is protected from untrusted
applications. The size of isolated storage is limited, so an
untrusted application cannot create a denial-of-service condition by
filling your hard disk with data.
IsolatedStorageScope).
If you use Windows 2000 or XP,
<SYSTEMDRIVE>\Documents and
Settings\<user>\Application Data contains the
isolated storage area if roaming is turned on, and
<SYSTEMDRIVE>\Documents and
Settings\<user>\Local Settings\Application Data contains the storage area if roaming is
not on. Applications can use this area as a data store for their particular
persistence needs. Figure 10-1 shows the
inheritance diagram for this namespace.
Normalize(),
which returns a normalized copy of the object on which it is
called. You usually use this method if you are inheriting from IsolatedStorage and
you want to see if a store already exists.
System.Net supports a high-level API for working with common Internet
protocols (HTTP being the principal example) without having to deal with
low-level details (such as the actual protocol format). In addition, this
namespace provides some high-level constructs for working with
networks — TCP/IP in particular.
WebClient
type, which provides the most high-level view of doing HTTP-style
request/response communications over a TCP/IP network (such as the
Internet), or else the slightly lower-level WebRequest
and WebResponse types. The choice between the two is
really not all that difficult — for most high-level, protocol-agnostic
work, WebClient will likely be the preferred choice.
If protocol-specific actions need to be taken (such as specifying additional
headers as part of an HTTP request, for example), then likely the
.NET
programmer will want to work with WebRequest and WebResponse. To be specific, the .NET programmer will
work with the concrete derived types HttpWebRequest and HttpWebResponse.
WebRequest and WebResponse must be written, an Abstract Factory type
implementing the IWebRequestCreate interface must be
created, and an instance of it (along with the protocol scheme prefix)
must be registered with WebRequest.RegisterPrefix().
Figure 11-1 shows the collaborations between
the concrete classes HttpWebRequest, HttpWebResponse, FileWebRequest,
and FileWebResponse.
ServicePoint class, and Figure 11-3 shows the remaining types in this namespace.
System.Net.Sockets namespace classes implement standard
Berkeley sockets APIs for cross-process/cross-host communication. Sockets are
low-level objects (abstractions, really) that provide the foundation for most Internet
networking. A socket binds to a local address and port, and either waits for
a connection from a remote address or connects to
a remote address and exchanges data across the network. Two socket implementations
are made available in this namespace, TCP/IP and UDP/IP. Most Internet applications,
such as FTP clients and web browsers, are built upon socket connections.
System.Net namespace) or the System.Runtime.Remoting types, to facilitate remote
communications.
If you need to work at the socket level, consider using TcpClient
or TcpListener. These are high-level abstractions of the socket API that support
client and server functionality.
System.Reflection is the API that exposes the full-fidelity
metadata of the .NET environment to the .NET programmer. In short,
it permits complete access to compile-time data at runtime. Everything is available,
including fields, methods,
constructors, properties, delegate types, and events.
The reflection API (as exposed by the System.Reflection
namespace) offers some truly unique capabilities unavailable in other
compile-time bound languages such as C++. The closest the average COM
programmer has come to using reflection is the IDispatch interface and/or type libraries.
Reflection, fortunately, is at once both easier to use and far more
powerful.
System.Reflection namespace. Reflection can also
be used for dynamic code creation, which is supported by the types in this
namespace, System.Reflection.Emit. Dynamic code creation
means a programmer can programmatically create code constructs such as methods
and events from within code, using the appropriate corresponding type (for
example, MethodBuilder and EventBuilder). These code
elements are all ingredients that can be added to a dynamic assembly,
represented by an AssemblyBuilder
object. Dynamic assemblies can be saved to disk as PE (Portable Executable)
files, typically in DLL form. Or, alternatively, emit it directly to memory
for immediate use, at the expense of persistence (memory-only types
disappear when the containing AppDomain terminates).
ILGenerator class allows you
to emit the MSIL (Microsoft Intermediate Language) for your code, using the
corresponding GetILGenerator() method from
a builder class. This process (sometimes known as
"baking") allows you to convert the information in the builder object into
a legitimate .NET type. You can then instantiate this newly created type on
the spot.
System.Reflection.Emit namespace is to
create compilers and script hosts,
although many other uses are possible, including programs that dynamically
create code that is fine-tuned to process a specific regular expression
(see System.Text.RegularExpressions.Regex.CompileToAssembly()).
When
creating dynamic types, you generally begin by creating an AssemblyBuilder, which contains one
or more ModuleBuilder objects. This
in turn contains TypeBuilder
instances. TypeBuilder objects contain
most of the other ingredients in this namespace, including classes for building
events, properties, methods, and enumerations.
offset
argument that specifies which element should be the first element
in the unmanaged array. If you want to pass the whole array,
specify an offset of zero.
System.Runtime.Serialization namespace and its
child namespace, System.Runtime.Serialization.Formatters
(with its own two child namespaces, System.Runtime.Serialization.Formatters.Binary,
and System.Runtime.Serialization.Formatters.Soap),
directly addresses these needs. System.Runtime.Serialization
contains the types necessary to perform the serialization of an object into
a stream of bytes, using an alternative object (which implements the IFormatter interface) to actually format the bytes
into either binary or XML form. While it is certainly feasible to write your
own custom formatters, most .NET programmers have no real practical need
to do so, since a binary format and an XML format cover most needs.
System.Runtime.Serialization.Formatters namespace,
which contains a number of types that are used by serialization
formatters.
Figure 17-1
shows the types in this namespace.
This chapter also features the BinaryFormatter
and SoapFormatter,
two formatters that live in their own namespace and rely on the types
in the System.Runtime.Serialization.Formatters
namespace.