RSS Feed




More...




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 blogger, author and a Kundalini Yogi who writes about apparently unrelated topics - Yoga & technology! A former Software Consultant and trainer by profession, Bipin is programming since 1995 and is working with .NET framework ever since its inception. He is an internationally published author and has authored or co-authored more than half a dozen books and numerous articles on .NET technologies. He has also penned a few books on Yoga. Bipin was also a Microsoft MVP for six consecutive years. You can read more about him here.

Stay updated : Twitter  Facebook  Google+


Associated Links
Download Source Code

Tags : VB.NET Programming Languages
Posted On : 06 Nov 2005
Current Rating :
Rate this product :


This page is protected by copyright laws. Copying in any form is strictly prohibited. For Copyright notice and legal terms of use click here.

Protected by Copyscape


Copyright (C) bipinjoshi.net. All rights reserved.
Contact Us
Read Copyright & Terms Of Use
Hosted By DiscountASP.net