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.
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 namespaceThe 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:
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 objectAnother 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: Response.Write(My.User.IsAuthenticated) Figure 2-6. Adding a web reference |