January 2018 : Instructor-led Online Course in ASP.NET Core 2.0. Conducted by Bipin Joshi. Read more...
Registration for January 2018 batch of ASP.NET Core 2.0 instructor-led online course has already started. Conducted by Bipin Joshi. Register today ! Click here for more details.

New Page 1

"My" Objects of VB.NET

Introduction

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.

"My" Namespace

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:

Object Purpose
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.

Example

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 & "." 
& My.Application.Info.Version.Revision
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.


Bipin Joshi is a software consultant, an author and a yoga mentor having 22+ years of experience in software development. He also conducts online courses in ASP.NET MVC / Core and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Ajapa Yoga to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 06 November 2005


Tags : VB.NET Programming Languages