|
|
|||
| H o m e | |||
|
|
O P T I O N S T R I C T O N By Michael McIntyre
Do you want to?
Speed up your code?
Reduce errors in release versions of your application?
Learn how to use VB.NET correctly?
If you answered yes to any of the questions above set OPTION STRICT ON in all your Visual Studio.NET VB.NET projects to gain speed, reduce runtime errors, and learn how to use VB.NET correctly.
This is a very important subject for VB.NET Newbies – and potentially a big trap if you fail to learn early on in your learning curve to set OPTION STRICT ON.
The reason this is potentially a trap is that by default VB.NET projects are set to OPTION STRICT OFF so that Visual Basic 6 projects can be imported into a VB.NET project. This default setting is for backward compatibility with legacy code. It is not meant to be used with new VB.NET code.
If you miss the many books, articles, and Microsoft help topics that advise you to turn OPTION STRICT ON you will begin your learning curve writing code that is slower and contains more runtime errors. Also, you will not get the feedback that the compiler can give you if OPTION STRICT ON is used, feedback which is essential to learning how to use VB.NET correctly. Here is a summary of the advice given:
Although Visual Basic .NET allows you to perform implicit type conversions and late binding, you should avoid these practices. Implicit type conversions and late binding may lead to severe performance problems, runtime errors, code that is difficult to read and maintain, and sub-standard programming practices. Many VB.NET professional programmers believe that leaving OPTION STRICT OFF by default was perhaps Microsoft's worst decision in the VB.NET implementation. Use OPTION STRICT ON.
Note: It is interesting to notice that the C# compiler automatically performs the same type checking that the VB.NET compiler only performs if OPTION STRICT is set to ON in a VB.NET project. C# is a new language and there is no legacy C# code to be upgraded to C#.NET. Because of this the dangerous option of turning strict type checking off is not a part of the C# language.
What does OPTION STRICT ON do? It forces a strong typing system in your code. It restricts implicit data type conversions to only widening conversions. This explicitly disallows any data type conversions in which data loss would occur and any conversion between numeric types and strings. It verifies that functions have return types and that all paths return a value. Some specific things OPTION STRICT ON forces to happen are:
Finds undeclared variables;
Ensures you do not accidentally use late binding. Late binding can still be done when absolutely necessary.
Prevents data loss when one variable is assigned to another that has less precision or capacity;
Provides compile-time notification when bad type conversions are tried.
I encourage you to go to the Visual Studio's Tools | Options menu, click the Projects \ VB Defaults folder and set Option Strict to On every time you start a new VB.NET project. You will spend a bit more time writing CType and DirectCast statements and you will be forced to learn how to use strong typing in VB.NET. But, your code will run faster, you will be a better VB.NET programmer, and you will avoid spending time tracking down mysterious runtime errors reported to you by your angry users.
Learn more by visiting the link below:
INFO: Option Explicit and Option Strict in Visual Basic .NET
|
||
|
Copyright © 2001-2003 aZ Software Developers. All rights reserved. |
|||