My, Oh My!

The .NET Framework is brilliant, but it can be quite complicated. Sometimes you just want to do something quickly, without configuring every single possibility and dealing with every possible situation. Microsoft introduced the My namespace in Visual Basic 2005 to make a simple way of doing complicated things. The My namespace makes it incredibly quick and easy to do certain common tasks, usually in just a single line of code.

The My namespace lets you quickly find information about the host computer. This could be checking the amount of physical RAM installed in the computer, for instance:

Dim mem As Long = My.Computer.Info.TotalPhysicalMemory()

My can also be used for checking whether the user is running as an Administrator, finding the My Documents folder, or a number of other things. Alternatively it can be used to perform tasks. For example, the .NET Framework has very powerful features for uploading files to the internet. However, this takes a lot of configuration code. If you want a quick way of uploading a file to a site, you could simply use:

My.Computer.Network.UploadFile(sourceFileName, address)

and do the whole process with a single line of code. UploadFile is overloaded, and different versions of the method can be used to upload files to password protected sites, and to display a progress dialog box while the file is uploading. (Several My methods give you a simple way of showing progress dialog boxes while they work, which  is a collossal timesaver.) There is also a DownloadFile method. The My namespace can be used for easy access to the computer's serial ports, which could be very useful to hobbyists who are working on automation or robotics.

With the old versions of BASIC, it was easy to use CSV files to store data. The My namespace lets you quickly create a TextFieldParser that can read your old CSV files, which is easier than using the .NET Framework functions.

One of the things that I especially liked about the My namespace was the ability to save application settings that would be available when the program was next used. This is done through My.Settings, which saves data in an XML configuration file, but you don't have to worry about XML as it's all hidden behind the scenes. The settings can be saved at any time on a per-user basis or set at compile-time on an application-wide basis. It is extremely easy to access these settings, and it was something I used a lot.

All of these things can be done with the .NET Framework, but the My namespace provides easy shortcuts for simple tasks. If you want to do something more complicated, you will have to write it yourself with .NET. Just to clarify, the My namespace is using .NET behind the scenes, so VB 2005 programs and components written using My are still fully compatible with all other .NET languages.

Next I will talk about using databases in VB 2005.