You might think of Visual Basic directives as the "other" Visual Basic. But as languages go, this one is pretty brief. It really consists of only four statements and only two actually affect the way the compiler creates output. This "language" was also available in VB 6 and worked about the same way.
Note ... with characteristic clarity,
Microsoft also uses the term
"directive" to describe the ASP.NET
commands, such as @ Page and @ Import.
These "directives" are quite different
and are not covered in this article.
Directives change the way the VB compiler creates the resulting program. The resulting compiled program never contains code from these statements, but different types of programs can be created by using them.
- #Const Directive
- #ExternalSource Directive
- #If...Then...#Else Directives
- #Region Directive
The #ExternalSource directive is used by the compiler to track line numbers for compiler error messages and the #Region directive is used to show or hide sections of code. It's not very likely that you will use them in your own code.
More good news: Directives also work in the VB.NET 2005 Express Edition (with one exception noted below)!
The idea behind the #If and #Const directives is conditional compiles. An example:
Suppose you have a program that must be sent out in slightly different versions for different customers. One way to do this would be to make a new copy of the program for each customer and then make the modifications in each copy. This approach take more space since you have to store each copy, but that's just a minor problem.
You could also do this with normal VB variables that are tested for custom output. While this would work, every compiled executable program would include the code for every customer. Even if this wasn't a security problem, it would make each programs a lot bigger and less efficient.
The most serious problem might be called, "code fragmentation". If you have a change that needs to be made to the core program, then you have to make the same change perfectly in all the copies. That could be a bit of work, but suppose you make a mistake in just one or two. That kind of problem can cost you customers and even more work. VB.NET directives make it possible to have just one copy and let the compiler make the changes for you!