You have probably noticed that the code snippets of Visual Studio use an entity called My, which is a peculiar object that was introduced with VB 2005 to simplify many programming tasks. As you saw in the preceding code snippet, the My object allows you to write some text to a file with a single statement, the WriteAllText method. If you’re familiar with earlier versions of Visual Basic, you know that you must first open a file, and then write some text to it, and finally close the file. The My object allows you to perform all these operations with a single statement, as you saw in the preceding example.
Another example is the Play method, which you can use to play back a WAV file from within your code:
My.Computer.Audio.Play ("C:\Sounds\CountDown.wav")Code language: VB.NET (vbnet)
You can also use the following expression to play back a system sound:
My.Computer.Audio.PlaySystemSound(System.Media.SystemSounds.Exclamation)Code language: VB.NET (vbnet)
The method that plays back the sound is the Play method, and the method that writes text to a file is the WriteAllText method. However, you can’t call them directly through the My object; they’re not methods of the My object. If they were, you’d have to dig hard to find out the method you need. The My object exposes six components, which contain their own components. Here’s a description of the basic components of the My object and the functionality you should expect to find in each component:
The Application component provides information about the current application. The CommandLineArgs property of My.Application returns a collection of strings, which are the arguments passed to the application when it was started. TypicalWindows applications aren’t called with command-line arguments, but it’s possible to start an application and pass a filename as an argument to the application (the document to be opened by the application, for example). The Info property is an object that exposes properties such as DirectoryPath (the application’s default folder), ProductName, Version, and so on.
This component of the My object exposes a lot of functionality via a number of properties, many of which are objects. The My.Computer.Audio component lets you play back sounds. The My.Computer.Clipboard component lets you access the Clipboard. To find out whether the Clipboard contains a specific type of data, use the ContainsText, ContainsImage, ContainsData, and ContainsAudio methods. To retrieve the contents of the Clipboard, use the GetText, GetImage, GetData, and GetAudioStream methods. Assuming that you have a form with a TextBox control and a PictureBox control, you can retrieve text or image data from the Clipboard and display it on the appropriate control with the following statements:
If My.Computer.Clipboard.ContainsImage Then
PictureBox1.Image = My.Computer.Clipboard.GetImage
If My.Computer.Clipboard.ContainsText Then
TextBox2.Text = My.Computer.Clipboard.GetText
End IfCode language: VB.NET (vbnet)
You may have noticed that using the My object in your code requires that you write long statements. You can shorten them substantially via the With statement, as shown next:
If .ContainsImage Then
PictureBox1.Image = .GetImage
If .ContainsText Then
TextBox2.Text = .GetText
End WithCode language: VB.NET (vbnet)
When you’re executing multiple statements on the same object, you can specify the object in a With statement and call its methods in the block of the With statement by specifying the method name prefixed with a period. The With statement is followed by the name of the object to which all following methods apply, and is terminated with the End With statement.
Another property of the My.Computer component is the FileSystem object that exposes all the methods you need to access files and folders. If you enter the expression My.Computer.FileSystem followed by a period, you will see all the methods exposed by the FileSystem component. Among them, you will find DeleteFile, DeleteDirectory, RenameFile, RenameDirectory, WriteAllText, ReadAllText, and many more. Select a method and then type the opening parenthesis. You will see the syntax of the method in a ToolTip. The syntax of the CopyFile method is as follows:
My.Computer.FileSystem.CopyFile(sourceFileName As String, destinationFileName As String)Code language: VB.NET (vbnet)
Just specify the path of the file you want to copy and the new file’s name, and you’re finished. This statement will copy the specified file to the specified location. You will notice that the ToolTip box with the syntax of the CopyFile method has multiple versions, which are listed at the left side of the box along with arrow up and arrow down icons. Click these two buttons to see the next and previous versions of the method. The second version of the CopyFile method is as follows:
sourceFileName As String, destinationFileName As String,
overwrite As Boolean)Code language: VB.NET (vbnet)
The overwrite argument specifies whether the method should overwrite the destination file if it exists.
The third version of the method accepts a different third argument that determines whether the usual copy animation will be displayed as the file is being copied. The various versions of the same method differ in the number and/or type of their arguments, and they’re called overloaded forms of the method. Instead of using multiple method names for the same basic operation, the overloaded forms of a method allow you to call the same method name and adjust its behavior by specifying different arguments.
This component lets you access the forms of the current application. You can also access the application’s forms by name, so the Forms component isn’t the most useful one.
This component lets you access the application settings. These settings apply to the entire application and are stored in an XML configuration file. The settings are created from within Visual Studio, and you use the Settings component to read them.
This component returns information about the current user. The most important property of the User component is the CurrentPrincipal property, which is an object that represents the credentials of the current user.
The WebServices component represents the web services referenced by the current application.
The My object gives beginners unprecedented programming power and allows you to perform tasks that would require substantial code if implemented with earlier versions of the language, not to mention the research it would take to locate the appropriate methods in the Framework. You can explore the My object on your own and use it as needed. My is not a substitute for learning the language and the Framework. It can help you initially, but you can’t go far without learning the methods of the Framework for handling files or any other feature.
Let’s say you want to locate all the files of a specific type in a folder, including its subfolders. Scanning a folder and its subfolders to any depth is quite a task (you’ll find the code in Chapter, ‘‘Accessing Folders and Files’’). You can do the same with a single statement by using the My object:
Dim files As ReadOnlyCollection(Of String)
files = My.Computer.FileSystem.GetFiles("D:\Data", True, "*.txt")Code language: VB.NET (vbnet)
The GetFiles method populates the files collection with the pathnames of the text files in the folder D:\Data and its subfolders. However, it won’t help you if you want to process each file in place. Moreover, this GetFiles method is synchronous: If the folder contains many subfolders with many files, it will block the interface until it retrieves all the files. In Chapter 15, you’ll see the code that retrieves filenames and adds them to a control as it goes along.
If you’re already familiar with VB, you may think that the My object is an aid for the absolute beginner or the nonprogrammer. This isn’t true. VB is about productivity, and the My object can help you be more productive with your daily tasks, regardless of your knowledge of the language or programming skills. If you can use My to save a few (or a few dozen) statements, do it. There’s no penalty for using the My object, because the compiler replaces the methods of the My object with the equivalent method calls to the Framework.