IIn Visual Basic
6 and earlier, we had a lot of "lazy" flexibility with variable
types. The VARIANT type would allow us to stick just about anything in
it and we even had the option of not declaring variables at all. Lazy
typing created a lot of - you guessed it -- lazy programmers. It also
created inefficiencies and a much higher probability of errors in code.
VB6 would then make assumptions about what we were trying
to do with our variables. In VB.NET we can still
do these things. VB.NET carries over the VB 6.0 declaration Option
Explicit, which requires us to declare all variables. VB.NET also
introduces the new declaration Option Strict,which forces
the strong CLR type system.
If we attempt to add two undeclared
variables with Option Strict turned off, the compiler generated IL contains
boxing code, moving the value types into the heap by sticking them into
a referenced type. Boxing is performed with System.Object, and there is
a performance hit because an additional object has to be maintained for
each undeclared variable. In addition, there will be a call to the Microsoft.Visualbasic
call object [Microsoft.VisualBasic]
There are cases when an instance of a value type needs
to be treated as an instance of a reference type. For situations like
this, a value type instance can be converted into a reference type instance
through a process called boxing. When a value type instance is boxed,
storage is allocated on the heap and the instance's value is copied into
that space. A reference to this storage is placed on the stack. The boxed
value is an object, a reference type that contains the contents of the
value type instance. A boxed value type instance can also be converted
back to its original form, a process called unboxing.
The key to this issue is understanding the difference
between value types and reference types, which is a fundamental distinction
in the Common type System, and this means having an understanding of how
memory is allocated for instances of each type. In managed code, values
can have their memory allocated either on the stack or on the heap, both
of which are managed by the CLR. Variables allocated on the stack are
normally created when a running method creates them. From a memory perspective,
the basic difference between value types and reference types is that an
instance of a value type has its value allocated on the stack, while an
instance of a reference type has only a reference to its actual value
allocated on the stack. The value itself is allocated on the heap.
The memory used by stack variables is automatically
freed when the method in which they were created returns. However,
variables allocated on the heap don't have their memory freed when
the method that created them returns. Instead, the memory used by these
variables is freed via the garbage collection process, which can (and
often does) occur whenever it "feels like it".
It is this second process, involving calling the Microsoft.VisualBasic
namespace, creating and boxing the new variables (the ones you didn't
declare and type) and finally storing references to these values on the
stack and the actual boxed values themselves on the heap is what happens
when you don't declare and type variables in VB.NET.
The bottom line is there is a definite
performance penalty for undeclared or untyped variables in VB.NET. With
Option Explicit On and Option Strict On, we must declare our two variables
and type them before attempting our addition. The two variables in the
compiler - generated IL are declared on the Stack with no boxing, which
is obviously less lines of IL code as well as much more efficient code.
Unfortunately, in bringing out
the final release of Visual Studio.NET, Microsoft bowed to the cries of
millions of VB programmers and left Option Strict Off by default in all
our VB.NET projects! Just about every professional author and programmer
will tell you that you should ALWAYS set Option Strict to "On"!
Fortunately, there is a very easy
way to do this so that you won't forget. Every VB.NET Project type has
a "ProjectType.vbproj" template that sets the basics for that
type of project. These are located under the "C:\Program Files\Microsoft
Visual Studio .NET\Vb7\VBWizards" folder, each in a separate subfolder.
All you need to do is use the Windows Explorer "Search" on the
above folder, searching on "*.vbproj" to bring them all up in
the right Explorer pane. Simply load each one in your favorite text editor,
and add the highlighted lines in red in the "Settings" section,
and re-save the files.
OutputType = "Library"
StartupObject = ""
OptionStrict = "On"
Now all your projects will automatically have "Option
Explicit" and "Option Strict" turned on. Trust me, there
may be a brief period of pain and inconvenience while you graduate to
the level of "non-crybaby" professional, but you will better
understand the .NET Framework and your programs will run faster, better
and with less errors!
a narrated Flash movie of how to perform the above modifications.
Peter Bromberg is an independent consultant specializing in distributed .NET solutions
Inc. in Orlando and a co-developer of the EggheadCafe.com
developer website. He can be reached at email@example.com