Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Visual Basic [VB] (http://www.go4expert.com/articles/visual-basic-tutorials/)
-   -   Constants in Visual Basic (http://www.go4expert.com/articles/constants-visual-basic-t3689/)

Sanskruti 30Mar2007 19:35

Constants in Visual Basic
 
A constant is a meaningful name that takes the place of a number or string that does not change. Constants store values that, as the name implies, remain the same throughout the execution of an application. You can greatly improve the readability of your code and make it easier to maintain by using constants. Use them in code that contains values that reappear or that depends on certain numbers that are difficult to remember or have no obvious meaning.

Visual Basic contains a number of predefined constants, mainly using for printing and displaying. You can also create your own constants with the Const statement, using the same guidelines you would for creating a variable name. If Option Strict is On, you must explicitly declare the constant type.

A constant's scope, which is the set of all code that can refer to it without qualifying its name, is the same as that of a variable declared in the same location. To create a constant that exists within the scope of a particular procedure, declare it inside that procedure. To create a constant that is available throughout an application, declare it using the Public keyword in the declarations section of the class.

Compile-time and Run-time Constants



A compile-time constant is computed at the time the code is compiled, while a run-time constant can only be computed while the application is running. A compile-time constant will have the same value each time an application runs, while a run-time constant may change each time. Compile-time constants are required for cases such as array bounds, case expressions, or enumerator initializers.

You use the Const statement to declare a constant and set its value. By declaring a constant, you assign a meaningful name to a value. Once a constant is declared, it cannot be modified or assigned a new value.

You declare a constant within a procedure or in the declarations section of a module, class, or structure. Class or structure-level constants are Private by default, but may also be declared as Public, Friend, Protected, or Protected Friend for the appropriate level of code access.

The constant must have a valid symbolic name and an expression composed of numeric or string constants and operators .

Write a declaration that includes an access specifier, the Const keyword, and an expression, as in the following examples:

Code: VB

Public Const DaysInYear = 365
Private Const WorkDays = 250

To declare a constant with Option Strict On



With Option Strict On, write a declaration that includes the As keyword and an explicit data type, as in the following examples:

Code: VB

Public Const MyInteger As Integer = 42 Private Const DaysInWeek As Short = 7 Protected Friend Const Funday As String = "Sunday"

You can declare multiple constants on a single line, although your code is more readable if you declare only a single constant per line. If you declare multiple constants on a single line, they must all have the same access level .

Separate the declarations with a comma and a space, as in the following example:

Code: VB

Public Const Four As Integer = 4, Five As Integer = 5, Six As Integer = 44

A constant is a meaningful name that takes the place of a number or string that does not change. Constants store values that, as the name implies, remain constant throughout the execution of an application. You can use constants that are defined by the controls or components you work with, or you can create your own. Constants you create yourself are described as user-defined.

A Const statement can represent a mathematical or date/time quantity:

Code: VB

Const conPi = 3.14159265358979 Public Const conMaxPlanets As Integer = 9  Const conReleaseDate = #1/1/1995#

It also can define String constants:

Code: VB

Public Const conVersion = "07.10.A" Const conCodeName = "Enigma"

The expression on the right side of the equal sign ( = ) is often a number or literal string, but it also can be an expression that results in a number or string (although that expression cannot contain calls to functions). You can even define constants in terms of previously defined constants:

Code: VB

Const conPi2 = conPi * 2

A Const statement's scope is the same as that of a variable declared in the same location. You can specify scope in any of the following ways:
• To create a constant that exists only within a procedure, declare it within that procedure.
• To create a constant available to all procedures within a class, but not to any code outside that module, declare it in the declarations section of the class.
• To create a constant that is available to all members of an assembly, but not to outside clients of the assembly, declare it using the Friend keyword in the declarations section of the class.
• To create a constant available throughout the application, declare it using the Public keyword in the declarations section the class

Steel9561 1May2008 03:01

Re: Constants in Visual Basic
 
I think that constants are kind of fun at the same time because of the fact that you have a specific value that never changes but that could possibly change in the future. For instance, currently you may have a constant for the tax rate you charge for specific products but let's suppose one year from now that tax rate changes then all you need to do is to change the value on that constant and calculations will be calculated properly Now, if you think that something will be constantly changing then I would definitely not use a constant but I would use a variable!

Luis Lazo


All times are GMT +5.5. The time now is 13:33.