The VB.NET Version 2.0 as well as C# version 2.0 contains many new enhancements to the core language. One of the new cool feature added to VB.NET is "My" objects. My objects is a set of objects that are implicitly available to your application which provide short cut to many tasks otherwise seem tedious. For example, to know details about the computer on which the application is running such as name, memory, clock and network you have My.Computer object. No need to look anywhere else. All the necessary information is available at a single place! In all there are seven My objects and this article is going to give you peek into some of them.
VB.NET 2.0 provides a namespace called "My" (Me, MyBase, MyClass and now My...hmm...what next?). This namespacfe provides seven implecit objects to your application. The following table lists all of them:
|My.Computer||Gives information about your computer such as its name, memory, clock, network details and so on.|
|My.Application||Gives information about the current application such as path, assembly information (name, version etc.) and environmental variables|
|My.User||Gives information about the current Windows user such as user name. Also, allows to check if user belongs to a specific role.|
|My.Forms||Gives reference to all the forms from a project. (I am sure VB6 developers are going to like this feature a lot)|
|My.WebServices||Gives access to proxy classes of the web services being used|
|My.Settings||Allows to read or store application configuration settings|
|My.Resources||Allows to read resources used by the application.|
As an example we are going to develop a Windows Forms application as shown in the following figure:
Here, we are displaying information about the computer, application and the user. We are also using an embedded resource. Displaying information about computer, application and user is straightforward. In order to retrieve embedded resource you must first add one to your application. In order to do so, double click on the Resources folder in the Solution Explorer. The interface of the resulting dialog is simple and you can easily embed image files using Add Resource toolbar option. Once you add required image file, rename the embedded resource as DotNetLogo. Finally, add the following lines of code to the Form_Load event:
Label2.Text = My.Computer.Name
Label4.Text = My.Computer.Network.IsAvailable
Label6.Text = My.Computer.Screen.DeviceName
Label8.Text = My.Computer.Clock.LocalTime
Label10.Text = My.Computer.Screen.Bounds.Width &
" X " & My.Computer.Screen.Bounds.Height
Label15.Text = My.Application.Info.DirectoryPath
Label16.Text = My.Application.IsNetworkDeployed
Label17.Text = My.Application.Info.AssemblyName
Label18.Text = My.Application.Info.Version.Major & "."
& My.Application.Info.Version.Minor & "."
& My.Application.Info.Version.Build & "."
Label19.Text = "Welcome " & My.User.Name & "!"
PictureBox1.Image = My.Resources.DotNetLogo
The code simply uses My.Computer, My.Application, My.User and My.Resources objects. That's it! Run the application and see the power of "My" objects in action.