My


In previous 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 can accomplish all of these tasks in previous versions of Visual Basic .NET, doing so is awkward.

To make these common tasks easier, Visual Basic 2005 introduced the My namespace to provide shortcuts for basic chores such as these. 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 Computer.FileSystem .ReadAllText method reads a text file in a single statement. The following statement reads the text in the file C:\Temp\Test.txt and displays it in a message box:

  Dim file_contents As String = _     My.Computer.FileSystem.ReadAllText("C:\Temp\Test.txt") 

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

This chapter 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. It makes sense to think of My.Computer as representing the computer.

My Sections

The following table briefly outlines the major sections within the My namespace. Other sections of this chapter and Appendix R describe these sections in greater detail.

Open table as spreadsheet

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 file system

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




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

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