You've filled your code with numeric values-and now it's time to change them all as you start work on the new version of the software! What a pain to have to track down and change all the numeric values throughout all the code. Isn't there a better way?
There is. Use constants and declare them all in one place. Then refer to the constants by name throughout the code instead of hardwiring numeric values in the code. When it's time to change those values, you just change the constants, all in one well-defined part of the code.
How do you use constants? You declare constants in Visual Basic with the Const statement, which you can use at the module, class, structure, procedure, or block level to declare constants for use in place of literal values:
[ <attrlist> ] [{ Public | Protected | Friend | Protected Friend | Private }] [ Shadows ] Const name [ As type ] = initexpr
Here are the various parts of this statement:
attrlist-A list of attributes that apply to the constants you're declaring in this statement. You separate multiple attributes with commas.
Public-Gives constants public access, which means there are no restrictions on their accessibility. You can use Public only at module, namespace, or file level (which means you can't use it inside a procedure). Note that if you specify Public, you can omit the Dim keyword if you want to.
Protected-Gives constants protected access, which means they are accessible only from within their own class or from a class derived from that class. You can use Protected only at class level (which means you can't use it inside a procedure), because you use it to declare members of a class. Note that if you specify Protected, you can omit the Dim keyword if you want to.
Friend-Gives constants friend access, which means they are accessible from within the program that contains their declaration, as well as anywhere else in the same assembly. You can use Friend only at module, namespace, or file level (which means you can't use it inside a procedure). Note that if you specify Friend, you can omit the Dim keyword if you want to.
Protected Friend-Gives constants both protected and friend access, which means they can be used by code in the same assembly, as well as by code in derived classes.
Private-Gives constants private access, which means they are accessible only from within their declaration context (usually a class), including any nested procedures. You can use Private only at module, namespace, or file level (which means you can't use it inside a procedure). Note that if you specify Private, you can omit the Dim keyword if you want to.
Shadows-Makes this constant a shadow of an identically named programming element in a base class. A shadowed element is unavailable in the derived class that shadows it. You can use Shadows only at module, namespace, or file level (but not inside a procedure). This means you can declare shadowing variables in a source file or inside a module, class, or structure, but not inside a procedure. Note that if you specify Shadows, you can omit the Dim keyword if you want to.
name-The name of the constant. You can declare as many constants as you like in the same declaration statement, specifying the name and initexpr parts for each one. You separate multiple constants with commas.
type-The data type of the constant. Can be Boolean, Byte, Char, Date, Decimal, Double, Integer, Long, Object, Short, Single, String, or the name of an enumeration. Note that you must use a separate As clause for each constant being defined. Note also that if type is Object, initexpr must be Nothing.
initexpr-An initialization expression. Can consist of a literal, another constant, a member of an enumeration, or any combination of literals, constants, and enumeration members.
Each attribute in the attrlist list must use this syntax:
<attrname [({ attrargs | attrinit })]>
Here are the parts of the attrlist list:
attrname-Name of the attribute.
attrargs-List of arguments for this attribute. Separate multiple arguments with commas.
attrinit-List of field or property initializers for this attribute. Separate multiple arguments with commas.
Here's an example showing how to declare and use a constant; in this case, I'm creating a constant named Pi, as well as Area and Radius variables, using the * (multiplication) operator to find the area of a circle, then converting that area from a number to a string with the Visual Basic Str function, and displaying the result:
Imports System.Console Module Module1 Sub Main() Const Pi = 3.14159 Dim Radius, Area As Single Radius = 1 Area = Pi * Radius * Radius WriteLine("Area = " & Str(Area)) End Sub End Module