The My Namespace and Assemblies


The 'My' Namespace and Assemblies

The .NET Framework, with its thousands of classes, contains a lot of packaged logic that I can use in my own programs. But I don't have all of the many assemblies and their classes memorized (yet), and it takes time to wander around the FCL documentation. With so many classes available, I sometimes shudder when I think of the effort it will take me to find just the right class or feature I need to accomplish some development task.

Fortunately, I'm not the only one who thinks this way; Microsoft agrees with me. Historically, Visual Basic programmers were sheltered from the complexities of Windows application development. Not that they needed to be; we all know that Visual Basic developers are generally a cut above the rest. But there was "the Visual Basic motto" to contend with: Make Windows Development Fast and Easy. And calling some esoteric method deep within the bowels of the System namespace just to get a minor piece of data is neither easy nor fast.

To bring back some semblance of the pleasant experience previously available in Visual Basic development, Microsoft introduced the My pretend namespace in its 2005 release of the language. The My pretend namespace collects a lot of useful features from all around the Framework Class Library, and organizes them in a much smaller hierarchy for simple and direct access. I briefly mentioned My in Chapter 1, but it's a good time to take a closer look at what it does.

The My pretend namespace looks a lot like other namespaces, such as System, System.Reflection, or System.Windows.Forms. But it's not really a namespaceit's pretend! For one thing, you can't use the Imports keyword to create a shortcut to branches within its hierarchy. Also, some sections of the hierarchy are dynamic; they change as your source code changes. Table 5-1 lists the major nodes of the hierarchy.

Table 5-1. Major Nodes in the My Namespace Hierarchy

Branch

Available Features

My.Application

Information about the current application, including culture settings and the deployment method.

My.Application.Info

Further details about the application and its assembly, including the name and version.

My.Application.Log

Allows you to generate trace and logging output to registered logging destinations. Only used with client applications.

My.Computer

Provides access to general resources located on the local computer.

My.Computer.Audio

Plays named and system sounds through the computer's speakers.

My.Computer.Clipboard

Retrieves data from the system clipboard, and lets you add your own data to the clipboard in a variety of predefined and custom formats.

My.Computer.Clock

Gets the current system date and time dished up in a variety of ways.

My.Computer.FileSystem

Tools to examine and manipulate files and directories on local or networked file systems.

My.Computer.FileSystem.SpecialDirectories

References to special Windows folders such as "My Documents," "Desktop," and "Temp."

My.Computer.Info

Provides information about the installed operating system and other local system resources.

My.Computer.Keyboard

Exposes the current state of the keyboard and its keys.

My.Computer.Mouse

Makes available a few properties of the local computer's mouse.

My.Computer.Network

Reports on network availability, and provides features to interact with that network.

My.Computer.Ports

Lets you interact with the system's serial ports.

My.Computer.Registry

Reads and writes keys and values in the registry.

My.Forms

Presents a dynamic collection of all forms defined in the application. This node is only available in Windows Forms applications.

My.Log

Allows you to generate trace and logging output to registered logging destinations. Only used with ASP.NET applications.

My.Request

This object is similar to the older Active Server Pages Request object. It is only available in ASP.NET applications.

My.Resources

Provides dynamic access to application-specific or locale-specific resources included with the application.

My.Response

This object is similar to the older Active Server Pages Response object. It is only available in ASP.NET applications.

My.Settings

Provides dynamic access to the new settings system included with Visual Basic 2005.

My.User

Identifies the current Windows user, including authentication information.

My.WebServices

Presents a collection of available Web Services for use in the application. This node is not available in ASP.NET applications.


The My namespace includes a lot of features you will use regularly, including access to the version number of the application. Instead of typing System.Reflection.whatever to get to the version number's "major" component, you can now just type:

My.Application.Info.Version.Major 


Need a list of assemblies, but you're too lazy to type the word "Reflection?" Try:

My.Application.Info.LoadedAssemblies 


Need to know the time right now in England?

My.Computer.Clock.GmtTime 


Can you communicate over the local area network?

My.Computer.Network.IsAvailable 


Who is running this computer anyway?

My.User.Name 


There isn't much in the My namespace that you can't already do with standard FCL libraries. There are even a few parts of My that are repeats of features already included in the Visual Basic language, although with some enhancements. For instance, Visual Basic includes a Kill command that lets you delete files. The new My.Computer.FileSystem.DeleteFile method also removes files, but it includes new options, including one that lets you send the file to the Recycle Bin instead of just losing it forever.




Start-to-Finish Visual Basic 2005. Learn Visual Basic 2005 as You Design and Develop a Complete Application
Start-to-Finish Visual Basic 2005: Learn Visual Basic 2005 as You Design and Develop a Complete Application
ISBN: 0321398009
EAN: 2147483647
Year: 2006
Pages: 247
Authors: Tim Patrick

Similar book on Amazon

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