The My Keyword
The My keyword is a novel concept that was introduced in the .NET Framework 2.0 to quickly give you access to your application, your users, your resources, the computer, or the network on which the application resides. The My keyword has been referred to as a way of speed-dialing common but complicated resources to which you need access. Using the My keyword, you can quickly access a wide variety of items, such as user details or specific settings of the requester browser.
Though not really considered a true namespace, the My object declarations that you make work in the same way as the .NET namespace structure you are used to working with. To give you an example, first look at how you get the user's machine name using the traditional namespace structure:
Environment.MachineName.ToString()
For this example, you simply need to use the Environment class and use this namespace to get at the MachineName property. The following shows how you would accomplish this same task using the My keyword:
My.Computer.Info.MachineName.ToString()
Looking at this example, you may be wondering what the point is if the example that uses My is lengthier than the first example, which just works off of the Environment namespace. Remember that the point is not the length of what you type to access specific classes, but a logical way to find frequently accessed resources without spending a lot of time hunting for them. Would you have known to look in the Environment class to get the machine ...
Get Professional Visual Basic 2012 and .NET 4.5 Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.