Hot-Link Menu in Right-Side Column

Last Updated 6-30-2010

How to work with Scope in Visual Basic

Programs written contain Variables, Constants and Arrays. In modern programming languages variables are only valid in a limited section of the program, and invalid or even non-existent in other parts. The part of the program that a variable is valid and can be used by the programmer is referred to as: Scope.

In order to help understand the concept of Scope a short synopsis for working with scope is given in the diagram below:

Scope Diagram
Visual Basic Variable Scope Description:

  • The scope of a variable determines what code has access to it.
    • Trying to access a scope outside of it's scope will cause a build error.

  • The scope of a variable is determined by where you declare it.
    • If you declare a variable within a procedure, it has Procedure Scope.
    • If you declare a variable within a class, but not within a procedure, it has Module Scope.

  • The lifetime of a variable is the period of time that the variable exists.
    • A variable declared within a procedure only exists while that procedure is executing.
    • A variable declared outside a procedure, but within a class, exists while the class exists.

Figure VB-47: Visual Basic Example of Module and Procedure Level Scope Variables

How to declare and use enumeration in Visual Basic

Often, when writing code, it is necessary to select an option, from a related group of options. For example, Customer payment terms could be Net 30 Days, Net 60 Days and Net 90 Days for different customers. If you assign a constant value such as '0' for 30 Days, '1' for 60 Days and '2' for Net 90 Days. In this case using Enumerations could simplify the programming process, and make your code more readable. For some examples on enumeration and the associated syntax, refer to the diagram below:

Visual Basic Syntax for Declaring an Enumeration:
	Enum EnumerationName [As Type]
		ConstantName1 [= value]
		ConstantName2 [= value]
	End Enum
Visual Basic Enumeration with Constant Values = 0, 1, 2
	Enum Terms
	End Enum
Visual Basic Enumeration with Constant Values = 30, 60, 90
	Enum TermValues
		Net30Days = 30
		Net60Days = 60
		Net90Days = 90
	End Enum
Visual Basic Statements Containing Enumerations
	Dim X As Integer = CInt( Terms.Net60Days )
	Dim Y As Integer = CInt( TermValues.Net90Days )
	Dim strTerm = "Terms: Net" + TermValues.Net30Days
	' X is 1
	' Y is 90
	' Terms: Net30
Visual Basic Enumeration Summary:
  • An enumeration defines a set of related constants.
    • Each constant is known as the member of the enumeration.

  • By default, an enumeration uses the Integer Data Type.
    • The first constant is 0, the second is 1, and so on.

  • To use one of the other Integer Data types you can code the As clause.

Figure VB-48: Visual Basic Enumeration Description, Syntax and Examples

How to Declare and use Nullable Types in Visual Basic

Occasionally, in programming it is necessary to know if a variable has had a value assigned to it yet, as in the case of whether or not a user has supplied requested information. The Null value refers to the concept of Nothing Yet as opposed to 0, the amount left in my wallet after my wife has gone shopping. The syntax and examples of Nullable Types are given in the diagram below:

Visual Basic Syntax for Declaring a Null Value Type:
	Dim VariableName As Nullable (Of Type) [= expression]
	Dim VariableName? As Type [= expression]
	Dim VariableName As Type [= expression]
Visual Basic Nullable Type Properties:

Property Description
HasValue Returns True if the nullable type contains a value, false if no value.
Value Returns the value of the Nullable type.

Visual Basic Nullable Types Examples:
	Dim NumberOfItems As Nullable (Of Integer)
	NumberOfItems = 20
	NumberOfItems = Nothing
	Dim InvoiceTotal? As Double
	Dim SomethingThere As Boolean = NumberOfItems.HasValue
	Dim ItemValue As Integer = NumberOfItems.Value
Visual Basic Nullable Type Summary:
  • A Nullable Type is a value type that can store a value.

  • Null Values are used to indicate the value of the variable is unknown.

  • To reset the value of a nullable type, use the Nothing keyword.

  • An arithmetic expression with a null value will place the null value as the result of the operation.
    • This is called Null Propogation Arithmetic.

Figure VB-49: Visual Basic Nullable Types Description, Syntax and Examples


Visual Basic

Introduction to VB.NET

.NET Framework

VS2008 IDE

How VB is Compiled

Start Visual Studio

Windows Form App

Save Your Work

VB OOP Programming

Visual Basic Code

Exit Code

Button Event Code

Coding Recommendations


Error List Window

Comment Syntax

Help Window

Language Essentianl

Built-In Data Types

Declare Variables

Declare Constants

Code Arithmetic Expressions

Assignment Statements

Operator Precedence

Type Casting

Math Class

String Declaration

Conversion Functions

Conversion Methods

Formatting Functions

String Formatting

Variable Scope


Nullable Types

Loop Constructs

For Next Loop

Do While Loop

Do Until Loop



Exit Do | Exit For


Nested Loops


Array Declaration

Rnd( ) Function

Listbox Control


Parallel Arrays

Key Event Args

Dynamic Arrays

Redimension Array

Set Breakpoint

Start Debugger

ReDim Preserve

MultiDimensional Arrays

DataGridView Control

Length and Sort Methods


Pad Right

Split Method

IsNumeric Function

Multiform Projects

Add Form To Project

Form Object Methods

Form Show Method

ShowDialog Method

Form Close Method

Form Accept Button

Multiform Project Example

ASP.NET Web Programming

Create Data Source

Configure Access Data Source

Add Product Class

Extract Local Database Data

Order PageLoad VB Code

Add New Web Page

Set Start Page

Display Cart Aspx Code

Display Cart Design View

Sorted List Definition

VB.NET Session State

Create CartItem Class

GetCartContents Function

Add To Cart Event Handler

Remove Cart Item Event

Clear Cart Event Handler