2.11. My Namespace

One of the problems faced by VB 6 programmers moving to VB 2005 is figuring out which class in the .NET Framework is the appropriate class to use to solve a particular problem. To simplify the transition, VB 2005 provides the new My namespace, which encapsulates some of the most common functionalities that developers need in their daily work.

VB 6 Tip: Most VB 6 predefined functions are still supported in VB 2005. They are located within the Microsoft. VisualBasic namespace (which is automatically referenced by default in all VB 2005 projects) and so you can continue to use your favorite VB 6 functions without doing anything extra.

The My namespace exposes several areas of functionality, as shown in the IntelliSense pop-up in Figure 2-4.

Figure 2-4. The objects exposed by the My namespace

The aim of the My namespace is to provide direct access to commonly used libraries (in the .NET Framework) like Resources that were previously difficult to access. The intuitive hierarchy of the My namespace provides a mechanism that VB 2005 developers can use to easily navigate the .NET Framework class libraries and locate the classes required to perform a particular task. For example, suppose you want to play an audio file in your application. Which class should you use? Using the My namespace, it is easy to locate the right class to use. As it turns out, the class to use can be found in My.Computer.Audio.Play!

The objects exposed by the My namespace are:


Provides properties, methods, and events related to the current application.


Provides properties for manipulating computer components, such as audio, the clock, the keyboard, the filesystem, and so on.


Provides access to the current user's security context. For Windows applications, the access is read-write, while access for web applications is read-only.


Provides properties for accessing an instance of each Windows Form declared in the current project.


Provides properties and methods for accessing the application's settings.


Provides properties for creating and accessing a single instance of each XML web service referenced by the current project.

The My namespace is not just a static shortcut to the class libraries in the .NET Framework. Depending on your project type, the My.Forms, My.Resources, My.Settings, and My. Webservices objects will dynamically display the relevant objects and classes.

Here are some examples of how to use the My namespace. You can use the My.Application object to discover the installation path of the current application:

 Dim appPath As String = _    My.Application.Info.DirectoryPath 

You can use the My.Computer object determine whether a file exists. At the same time, you can also play a system audio sound:

 Dim exists As Boolean exists = _    My.Computer.FileSystem.FileExists( _    "c:\file.txt") If Not exists Then        My.Computer.Audio.PlaySystemSound( _           System.Media.SystemSounds.Exclamation)        MsgBox("File does not exist")     End If 

You can also play a specific audio file:

 My.Computer.Audio.Play( _        "C:\WINDOWS\Media\chimes.wav") 

File management is one of the most common tasks that developers need to perform. Using the My.Computer.FileSystem object, you can access all the various file handling routines in one place (see Figure 2-5).

Figure 2-5. The routines in the My.Computer.FileSystem object

Another useful object that resides in My.Computer is the Network object. With it, you can perform a task such as downloading a file from the network and saving it locally. The following example downloads a .gif file from a web site and saves to your local C: drive.

      My.Computer.Network.DownloadFile( _          "http://www.oreilly.com/catalog/" & _  "covers/aspnetadn.s.gif", _  "c:\images\0596008120.jpg") 

In a Windows application, you can access the collections of forms in your application and their properties with the My.Forms object. For example, the following statements set the Opacity property of a form to 50%:

 My.Forms.Form1.Opacity = 0.5 ' ---equivalent to--- Form1.Opacity = 0.5 

If you have multiple web services references in a project, you can find them all in the My.WebServices object. For example, suppose you have added a web reference to the Translate web service in your application (see Figure 2-6) at http://www.webservicex.net/TranslateService.asmx?WSDL. The following example shows how to invoke the translateService web service through the My.WebServices object:

 MsgBox(My.WebServices.TranslateService.Translate( _         net.webservicex.www.Language.EnglishTOFrench, "Hello")) 

In a web application, you can use My.User to determine whether a user is authenticated:


Figure 2-6. Adding a web reference

Visual Basic 2005 Jumpstart 2005
Visual Basic 2005 Jumpstart
ISBN: 059610071X
EAN: 2147483647
Year: 2005
Pages: 86
Authors: Wei-Meng Lee

Similar book on Amazon

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