Tuesday, July 20, 2004

The My namespace

In Whidbey, VB.NET provides a new feature called “MY” Namespace for rapid application development and to improve productivity. Through this one can access the information about the current application (Using the functionality My.Application), the information about the computer that the application is installed on (Using the functionality My.Computer) and the information about the current user of the application (Using the functionality My.User).
 
My Object Model: (The My Namespace)

 
My.Application: By using this you can get information about the running application such as the title, current directory, and version. It also gives access to environment variables allowing you to easily write to the local application log, or write to a custom log, etc
 
My.Computer: By using this you can get information about the underlying platform and the hardware on the local machine that the application is running on. For instance, Registry, Printer, Keyboard, Screen, etc. are types of objects you can access through this class.
 
My.User: By using this you can get information about the current user in terms of their display name, domain name, what groups the user is a member of, and so on.
 
My.WebServices: It allows you to easily access XML Web services that are used in your application.

 
My.Forms: By using this you can get a collection of all the instances of the forms in your current project. Allows you to easily show and hide forms without having to explicitly create an instance of the form in code.

Download this sample chapter on the My Namespace, as excerpted from Microsoft Visual Basic Programmer’s Introduction to Whidbey”, by Sean Campbell, Scott Swigart, Kris Horrocks, Derek Hatchard, and Peter Bernhardt.

In short instead of using:

System.Windows.Forms.SystemInformation.MousePresent()

you can use:

My.Computer.Mouse.Exists()

To find whether your machine is connected to a Mouse or Not.

Few more examples from Insert Snippet:

This example plays a sound from a file:

My.Computer.Audio.Play("ringout.wav", AudioPlayMode.WaitToComplete)

This example provides the available free space in the drive:

Dim drive As System.IO.DriveInfo
drive = My.Computer.FileSystem.GetDriveInfo("C:\")
Dim space As Long = drive.AvailableFreeSpace

To clear the Clipboard:

My.Computer.Clipboard.Clear()

Old Method of declaration and instantiation:

Dim myForm As New Form1
myForm.Show

With My.Forms, you can directly call methods on the default instance:

My.Forms.Form1.Show

Intersting Isn't? What do you guys think about this feature? Share your thoughts here!

But rightn ow this is only available in VB .NET. I am looking this feature on C# too.


No comments: