News

Internet - .Net user group technical events, emerging .Net technologies;
General - Eco-travel, health and fitness, current events;
Community - Active volunteer with Hands On Miami, Non-Profit Ways;
.Net Framework - Detected 3.5 SP1 .NET Framework. No update needed ;
creative zen converter

Tweets













Visual Basic 2005

Visual Studio 2005 and Visual Basic 2005 add many features and tools that make your development experience more productive. The following sections outline just a few.

The My Namespace

Imagine being able to find the functionality you need within the huge set of classes available as part of the .NET Framework immediately. Imagine using a single reference to accomplish goals that would otherwise require many lines of code. Imagine being more productive than you could ever have been programming in previous versions of Visual Basic or Visual Basic 6. These goals, and more, have been met with the addition of the My namespace to Visual Basic 2005.

Like shortcuts on your Windows desktop make it easier for you to find the applications and files you need in Windows, the My namespace classes provide shortcuts to commonly used functionality within the .NET Framework. In addition, the My namespace adds functionality that was previously difficult to find, difficult to use, or was simply not possible without unmanaged API calls.

The My namespace includes the following classes, each of which includes a number of useful members:

  • Application
  • Computer
  • Forms
  • Resources
  • Settings
  • User

For example, to play an audio file in Visual Basic 2005, rather than using DirectX or Win32 API calls, you could write this simple single line of code:

My.Computer.Audio.Play("C:\Beep.wav")

Or, to play a system sound, you might write code like this:

My.Computer.Audio.PlaySystemSound(SystemSounds.Asterisk)

In addition, My namespace supplies functionality that otherwise requires substantial code. For example, the following code demonstrates how you can use simple code to ping a computer:

If My.Computer.Network.IsAvailable Then
    If My.Computer.Network.Ping("http://www.microsoft.com") Then
        MsgBox("Microsoft's site is available.")
    End If
End If

Working with the file system has never been easier. My.Computer.FileSystem provides a flat, stateless, discoverable, and easy-to-use way to perform common file system operations. File system operations such as copying, deleting, moving, and renaming files and folders are at the core of My.Computer.FileSystem. For example, developers frequently require file, folder, and drive properties (such as a file's size, encoding, and so on). Using My.Computer.FileSystem, commonly referenced file, folder, and drive properties can be obtained easily. For example, to display the size of each drive in your system, you might write code like the following:

For Each drv As DriveInfo In My.Computer.FileSystem.Drives
    If drv.IsReady Then
        Debug.WriteLine(String.Format( _
         "{0}:\ {1:N0}", drv.Name, drv.TotalSize))
    End If
Next

The My.User class provides information about the current user, including Name and IsInRole properties. You could use code like the following to display the current user's information and whether the user is an administrator:

MsgBox(My.User.Identity.Name & ":" & _
    My.User.IsInRole("Administrators"))

If you wanted to determine the current user's application data folder, you could use simple code like the following:

MsgBox( _
My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData)

The My namespace also adds functionality that returns many of the RAD features of Visual Basic 6 to the .NET development platform. For example, Visual Basic developers historically accessed forms by name, relying on the runtime engine to maintain a collection of all the available form classes. Using the new My.Forms collection, developers can write code to open and interact with an instance of a form class created as part of a solution, like the following:

My.Forms.HelpForm.Show() 

In addition, the My namespace includes a number of other dynamically created collections, including Forms, WebServices, Resources, and Settings. The My.WebServices collection allows developers to easily call Web Service methods taking advantage of IntelliSense and strong typing, and makes the call simpler as well. For example, imagine a Web Service named Numbers that has already been declared in a project. Calling the NumToStr method of the Web Service couldn't get much easier than this:

MsgBox(My.WebServices.Numbers.NumToStr(123.45))

The My namespace has been devised to make it simpler for Visual Basic developers to accomplish their goals with less work, and with less searching through multiple namespaces. You'll find that many arduous tasks in previous releases are now only a single reference away.

IntelliSense and Code Snippets

Visual Basic 2005 is more aggressive than previous versions when it comes to warning you of coding errors. For example, attempting to use a variable before it has been assigned a value triggers a warning, as shown in Figure 1.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
Friday, June 02, 2006 3:27 PM

Feedback

No comments posted yet.


Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: