Top Five Visual Basic .NET Tips
Pages: 1, 2
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."  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:
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).
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.
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.
Showing messages 1 through 1 of 1.
A beginner of VB.NET
2002-11-20 18:26:41 anonymous2 [View]