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.
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:
With Option Strict On, write a declaration that includes the As keyword and an explicit data type, as in the following examples:
Separate the declarations with a comma and a space, as in the following example:
A Const statement can represent a mathematical or date/time quantity:
• To create a constant that exists only within a procedure, declare it within that procedure.
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!
|All times are GMT +5.5. The time now is 09:04.|