Top Five Visual Basic .NET Tipsby 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.
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.
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.
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.
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:
|Do you have VB .NET tips of your own?|
In addition to these resources, consider joining (or starting!) a local .NET developers group.
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."  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.
Three features are generally necessary for a language to be considered fully object oriented:
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.
Different objects can be referenced and manipulated without regard to the specific types of the objects. Visual Basic 6 had this feature.
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).
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.
Option Explicit statement determines whether the compiler requires all variables to be explicitly declared. The syntax is:
Option Explicit [ On | Off ]
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 ]
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
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.
 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.
Sample Chapter 8, ADO.NET: Developing Database Applications, is available free online.
For more information, or to order the book, click here.
Copyright © 2009 O'Reilly Media, Inc.