Published on ONDotNet.com (http://www.ondotnet.com/)
 See this if you're having trouble printing code examples


Top Five Visual Basic .NET Tips

by Dave Grundgeiger, author of Programming Visual Basic .NET

The .NET Framework and Visual Basic .NET represent a huge departure from the Visual Basic 6 programming environment. Some developers may see this as daunting, but I see it as exciting. The changes that Microsoft has made to Visual Basic have been made for the sole purpose of making it a better tool for writing professional applications in the coming years. I think they have succeeded. Every time that I dig down to find out how "X" is done in .NET, I'm always pleasantly surprised by the simplicity and elegance of the solution. I think you will be too. Here are my top five tips on how to get comfortable with Visual Basic .NET.

1. Spend time with the .NET Framework classes

The hard part about moving up to Visual Basic .NET is not the language, but learning to use the types provided in the .NET Framework class library. The class library wraps all of the functionality provided by the Windows operating system and the .NET common language runtime. It provides types for creating GUI applications, Web apps, and Web services. It provides code for accessing data in SQL Server and other database, code for working with regular expressions and XML, code for low-level network access, and so on. In fact, there are about 4,000 types defined in the .NET Framework class library. The good news is that all .NET languages access the .NET Framework class library in the same way, meaning that once you've mastered the class library, you can move from language to language with relative ease.

So, how do you master the .NET Framework class library? Start with a good class browser. All .NET types are self-describing, and a number of .NET enthusiasts have written applications for browsing type information. There is a very good one available called Reflector, written by Lutz Roeder at Microsoft, which can be downloaded for free from his Programming .NET Web site.

Related Reading

Programming Visual Basic .NETProgramming Visual Basic .NET
By Dave Grundgeiger
Table of Contents
Sample Chapter
Full Description

With Reflector you can:

In addition, there are plans for adding the ability to reverse-engineer source code in C#.

Another great tool is Anakrino, written by Jay Freeman. It too can be downloaded for free. In addition to being able to browse Microsoft and third-party assemblies and show disassembled IL, Anakrino can actually decompile IL code into C# code. That is, it generates C# code that, if compiled, would produce the IL that was fed to it (regardless of the language in which the component was originally written). Unfortunately, at the time of this writing, there are no plans to add a Visual Basic .NET decompiler to the product.

2. Use the Web

You are not alone! There is an amazing amount of brainpower floating around in cyberspace. If you are stuck, you need never stay stuck for long. Here are some of my favorite hangouts on the Web.

.NET Information

This is the official home page for information about Microsoft's .NET initiative. On this page you will find links to:
Microsoft Developer Network (MSDN)
This offers complete .NET reference documentation online.
Microsoft's GotDotNet
.NET articles, samples, and links are provided by Microsoft .NET team members, as well as users.
Microsoft's IBuySpy
This is a reference implementation for a complete Web site developed using ASP .NET. Complete source code is downloadable in Visual Basic .NET or C#.
O'Reilly has a large lineup of .NET books written by experts in the field.
Ron's VB Forum
O'Reilly editor Ron Petrusha hosts this column in which particularly interesting VB questions are answered.
DevX .NET Developer Resources
This is a great list of links to a huge number of .NET resources.

Discussion Lists

Discussion lists are one of the greatest resources on the Internet. These give you the opportunity to ask questions of tens, hundreds, or thousands of people who are in your field of interest. The Web URLs listed here link to pages that provide instructions for subscribing to the lists.


If you've never before been a member of a discussion list, take care to note the concept of netiquette. This term refers to the rules of behavior expected from people who post messages to discussion lists. You will receive or be directed to a list of such expectations when you subscribe to a list, but they can be summed up as follows:

  1. Monitor the discussion list a while before posting messages.
  2. If available, search the list archives before posting a question.
  3. Don't post questions that are outside of the list's discussion topic.
  4. Be nice.
  5. Answer the questions that you can.
  6. Maximize your signal-to-noise ratio. (That means you should post more helpful answers than witty remarks.)

DevelopMentor's DOTNET List
This is my all-time favorite .NET hangout. There are many heavy hitters on this list, including Microsoft development team members and program managers as well as third-party experts who were learning and using .NET as much as a year or two before it's official release.
ASP Lists
This site is amazing for its breadth of coverage. There are over 400 highly focused, moderated lists available through this one site. Many of these are now .NET-related, and many are available in languages other than English.
Visual Basic List
The members of this list discuss everything related to Visual Basic, including Visual Basic .NET and earlier versions. This is a large, very active list with developers of all skill levels. There are plenty of advanced Visual Basic developers on this list who are ready to lend a hand when they can. There are approximately 4,400 subscribers.

Comment on this articleDo you have VB .NET tips of your own?
Post your comments

Visual Basic Beginner's List
For beginners who don't feel comfortable posting to the Visual Basic List, there is the Visual Basic Beginner's List. There are approximately 1,000 subscribers.

In addition to these resources, consider joining (or starting!) a local .NET developers group.

3. Think about your business in terms of Web services

This is equally important for developers and business executives. Take a look around Microsoft's Web site, particular the stuff that talks about .NET. Microsoft says, "Everything needs to be a Web service." [1] Microsoft is asking itself how it can prosper in the new world of Web services, and .NET My Services is the result. (See http://www.microsoft.com/myservices/.) Your company must do the same.

4. Read a good book on object-oriented design

Three features are generally necessary for a language to be considered fully object oriented:

  1. Encapsulation

    The data that represents the state of an object can be manipulated through only public methods provided by the object for that purpose. How the object represents state and the implementation details of its methods is hidden from the rest of the program. Visual Basic 6 had this feature.

  2. Polymorphism

    Different objects can be referenced and manipulated without regard to the specific types of the objects. Visual Basic 6 had this feature.

  3. Inheritance

    A class can be defined to "inherit" the implementation of another class. This means that objects of the newly defined class have the same behavior as objects of the "parent" class. The newly defined class can then have additional methods defined within it, or inherited methods can be replaced with new implementations. This is the feature that previous versions of Visual Basic were lacking.

    Inheritance is a powerful tool. Like any powerful tool, it can be grossly misused. I strongly suggest that if you don't have experience with object-oriented design, you should study up on this field. A good place to start is with The Object Primer, by Scott W. Ambler (Cambridge University Press).

5: Use Option Explicit On and Option Strict On--always

Another way to say this is, "prefer compile-time error checking over runtime error checking." When a program successfully compiles, you know that it has successfully passed all compile-time checks--no compile-time checks will later turn up to haunt you. In contrast, just because a program runs without error once doesn't mean that it will run without error the next time. Consequently, applications are more robust if potential runtime errors are instead caught at compile time. Option Explicit On and Option Strict On help you do just that.

The Option Explicit statement determines whether the compiler requires all variables to be explicitly declared. The syntax is:

	Option Explicit [ On | Off ]

If On is specified, then the compiler requires all variables to be declared. If Off is specified, then the compiler considers a variable's use to be an implicit declaration. This is a bad thing, because variable names with typographical errors in them won't cause errors at compile time (because their use counts as an implicit declaration), but behavior will be unpredictable at runtime. There is never any reason to specify Option Explicit Off.

The Option Strict statement controls the implicit type conversions that the compiler will allow. The syntax is:

	Option Strict [ On | Off ]

If On is specified, the compiler only allows implicit widening conversions; narrowing conversions must be explicit. If Off is specified, then the compiler allows implicit narrowing conversions as well. This could result in runtime exceptions not foreseen by the developer.

So just what are "widening conversions" and "narrowing conversion"? Widening conversions are conversions in which there is no possibility for data loss or incorrect results. For example converting a value of type Integer to type Long is a widening conversion because the Long type can accommodate every possible value of the Integer type. The reverse operation--converting from a Long to an Integer--is a narrowing conversion, because some values of type Long can't be represented as values of type Integer.

Visual Basic .NET performs widening conversions automatically whenever necessary. For example, a widening conversion occurs automatically in the second line of the following code. The Integer value on the right-hand side of the assignment is automatically converted to a Long value so it can be stored in the variable b:

	Dim a As Integer = 5
	Dim b As Long = a

A conversion that happens automatically is called in "implicit conversion."

Now consider the reverse situation:

	Dim a As Long = 5
	Dim b As Integer = a

The second line of code here attempts to perform an implicit narrowing conversion. Whether the compiler permits that line of code depends on the value set for the Option Strict compiler option. When Option Strict is On, attempts to perform an implicit narrowing conversion result in a compiler error. When Option Strict is Off, the compiler automatically adds code behind the scenes to perform the conversion. At runtime, if the actual value being converted is out of the range that can be represented by the target type, then a runtime exception occurs.

It is sometimes the case that a narrowing conversion is required for normal program flow. Perhaps the developer knows that a certain variable will never hold a value that is invalid for the target type, or perhaps the programmer has added code to handle the exception that will occur if the value is invalid for the target type. In this case, an explicit conversion can be performed by using an appropriate conversion function. For example:

	Dim a As Long = 5
	Dim b As Integer = CInt(a)

This is known as an explicit conversion because the programmer is explicitly requesting a conversion to Integer. Note that although the syntax makes it appear that this is an additional function call, the conversion is actually compiled inline, resulting in a conversion that is as efficient as an implicit conversion.

[1] Sanjay Parthasarathy, vice president of Platform Strategy, Microsoft Corporation, December 21, 2000, in the whitepaper, "The Simplest Way to Define .NET" (http://www.microsoft.com/net/define_net.asp).

O'Reilly & Associates recently released (December 2002) Programming Visual Basic .NET.

Copyright © 2009 O'Reilly Media, Inc.