Scope

You will remember that in Visual Basic 2005 objects declared within methods are called local variables. They are local to the method, as opposed to belonging to the object, as member variables do.

Local variables of intrinsic types such as Integer are created on the "stack." The stack is a portion of memory that is allocated as methods are invoked and freed as methods end. When you start a method, all the local variables are created on the stack. When the method ends, all the local variables are destroyed.

These variables are referred to as local because they exist (and are visible) only during the lifetime of the method. They are said to have local scope . When the method ends, the variable goes out of scope and is destroyed.

.NET divides the world of types into value types and reference types. Value types are created on the stack. All the intrinsic types (Integer, Long, etc.) are value types and thus are created on the stack.

Classes, on the other hand, are reference types. Reference types are created on the heap.

Get Programming Visual Basic 2005 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.