Browse DevX
Sign up for e-mail newsletters from DevX


What's New in Visual Basic 2005? : Page 2

Going forward, Microsoft will make a big push to migrate Visual Basic 6 developers to Visual Studio 2005. The Visual Basic team has addressed many of the areas that caused VB6 developers to hold on to their old code base.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

The "My" Namespace
One of the shocks that VB6 programmers encountered when they migrate to Visual Basic .NET is the sheer number of class libraries available. VB6 programmers often struggle with finding the right class for the right task, and to them this task can be like finding a needle in a haystack.

To ease the pain of VB6 programmers, Microsoft has added the new My namespace in Visual Basic 2005.

The My namespace:

  • Provides shortcuts to commonly used functionality within the .NET Framework
  • Adds functionality that was previously difficult to find, difficult to use, or was simply not possible without unmanaged API calls
Figure 1. My Namespace: The figure shows the objects in the My namespace.
My includes the following objects (see Figure 1).

  • Application
  • Computer
  • Forms
  • Resources
  • Settings
  • User
  • WebServices
For example, to get the computer to play a system sound, you can directly use the My.Computer object.

My.Computer.Audio.PlaySystemSound( _ SystemSounds.Exclamation)

Contrast this to using the Win32 API to play audio, or using the Audio class within the Microsoft.VisualBasic.Devices namespace. (Who would have thought that this library was located in this namespace?)

Dim audio As New _ Microsoft.VisualBasic.Devices.Audio() audio.PlaySystemSound( _ SystemSounds.Exclamation)

Another example of using the My namespace is downloading files from the Internet. The following statement downloads an image from the Web and then saves it onto your local drive:

My.Computer.Network.DownloadFile( _ "http://www.code-magazine.com/ images/code_logo_white.gif", _ "C:\Images\log.gif")

Partial Classes
In Visual Basic 2005, you can now split the definition of a class into multiple physical files using the new Partial keyword.

For example, you could rewrite the MyString class illustrated earlier as two separate partial classes (see Listing 2).

Note that in Visual Basic 2005, every partial class file except one from the complete set of partial class files that make up a class needs the Partial keyword.
Figure 2. Designer-generated Code: The designer-generated code for a Windows Form saved in a separate file.
Using partial classes you can now try to separate a class definition into multiple files, perhaps one class for containing all the properties definitions and another class for all the methods. To the compiler, the partial classes are treated as one single class.

Partial classes are useful for separating designer-generated code and business logic. For example, in Visual Studio .NET 2003, the designer-generated code for a Windows Form is embedded within the code-behind for the form. This often creates a mess when developers unknowingly modify the generated code. In Visual Studio 2005, the designer-generated code for WinForms are all saved in a separate partial class (see Figure 2), so your code behind for your Windows form is now clear of all the clutter (see Figure 3).

Figure 3. Code Behind: The figure shows the code behind of a Windows Form.
Apart from using partial classes to separate business logic from UI, you can also use partial classes to add extra methods to classes that are auto generated, e.g., a typed dataset. You can then regenerate the auto generated part of the file without breaking the extra methods.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date