MY

In older versions of Visual Basic .NET, programmers discovered that many common tasks were difficult to perform. For example, many programs get the name of the user logged on to the computer, read a text file into a string, get the program’s version number, or examine all of the application’s currently loaded forms. Although you could accomplish all of these tasks in early versions of Visual Basic .NET, doing so was awkward.

To make these common tasks easier, the My namespace was introduced to provide shortcuts for basic chores. For example, to read the text in a file in Visual Basic .NET 2003, you must create some sort of object that can work with a file such as a StreamReader, use the object to read the file (the ReadToEnd method for a StreamReader), and then dispose of the object. The following code shows how you might do this in Visual Basic .NET 2003:

Dim stream_reader As New IO.StreamReader(file_name)
Dim file_contents As String = stream_reader.ReadToEnd()
stream_reader.Close()

This isn’t too difficult, but it does seem more complicated than such a simple everyday task should be.

The My namespace provides a simpler method for reading a file’s contents. The My.Computer.FileSystem.ReadAllText method reads a text file in a single statement. The following statement reads the text in the file C:TempTest.txt:

Dim file_contents As String =
    My.Computer.FileSystem.ReadAllText("C:TempTest.txt")

There is nothing new in the My namespace. All the tasks it performs you can already handle using other methods. The My namespace just makes some things easier.

This section describes the My namespace and the shortcuts it provides.

Me and My

Some programmers confuse the Me object and the My namespace. Me is a reference to the object that is currently executing code. If a piece of code is inside a particular class, Me is a reference to the class object that is running.

For example, if the class is a form, then within the form’s code, Me returns a reference to the running form. If the form’s code must change the form’s BackColor property, it can use the Me object to explicitly refer to its own form. It can also omit the keyword to refer to its form implicitly. That means the following two statements are equivalent:

Me.BackColor = SystemColors.Control
BackColor = SystemColors.Control

If you build several instances of a class, the code in each instance gets a different value for Me. Each instance’s Me object returns a reference to that instance.

On the other hand, My isn’t an object at all. It is a namespace that contains objects, values, routines, and other namespaces that implement common functions. The My namespace is a single unique entity shared by all of the code throughout the application.

It may help if you try not to think of the My namespace as a thing in and of itself. The My namespace doesn’t do anything all alone. It needs to be paired with something within the namespace. Think of My.Application, My.User, My.Computer, and so forth.

My Sections

The following table briefly outlines the major sections within the My namespace. Other sections of this chapter and Appendix S, “The My Namespace,” describe these sections in greater detail.

SECTION PURPOSE
My.Application Provides information about the current application: current directory, culture, and assembly information (such as program version number, log, splash screen, and forms)
My.Computer Controls the computer hardware and system software: audio, clock, keyboard, clipboard, mouse, network, printers, registry, and filesystem
My.Forms Provides access to an instance of each type of Windows Form defined in the application
My.Resources Provides access to the application’s resources: strings, images, audio, and so forth
My.Settings Provides access to the application’s settings
My.User Provides access to information about the current user
My.WebServices Provides access to an instance of each XML web service referenced by the application
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.49.108