Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. User-Defined Constants (Visual Basic)
In this articleA 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. You declare a constant with the Const Statement UsageA
It also can define
The expression on the right side of the equal sign (
Scope of User-Defined ConstantsA
For more information, see How to: Declare A Constant. Avoiding Circular ReferencesBecause constants can be defined in terms of other constants, it is possible to inadvertently create a cycle, or circular reference, between two or more constants. A cycle occurs when you have two or more public constants, each of which is defined in terms of the other, as in the following example:
If a cycle occurs, Visual Basic generates a compiler error. See also
FeedbackSubmit and view feedback for Identifiers, Variables, and ConstantsIdentifiersAn identifier is an unique name given to an entity, which distinctly identifies an entity in a program at the time of its execution. Identifiers are used for the naming of variable, function, class, structure or a constant, etc. Once an identifier is declared, we can use the identifier anywhere in the program to refer to the associated value. The general rules for constructing unique identifiers are:
Valid and Invalid IdentifersIdentifer naming conventions
VariablesA variable is a named storage location in the computer's memory for holding a piece of information. The information stored in variables may change while the program is running (hence the name "variable"). Variables are symbolic names that represent locations in the computer's random-access memory (RAM). When information is stored in a variable, it is actually stored in RAM. Syntaxdata_type variable_name = value; Variable declarationTake a look at this: int age; This is called variable declaration. It tells the compiler the variable's name and the data type it will hold. Memory is allocated upon variable declaration. This line indicates the variable's name is age. The word int stands for integer, so age will only be used to hold integer numbers. The data type also determines the amount of memory space to be allocated for the variable. Variable declaration and initializationTake a look at this: int age = 20; This is called variable declaration and initialization. It provides a inital value 20 to age, which will be saved in the allocated memory location. Usually, variable declaration and initialization are done together. NOTE:
Constants refer to as fixed values, unlike variables whose value can be altered, constants - as the name implies does not change, they remain constant. Constant must have to be initialized at the time of creating it, and new values cannot be assigned later to it. Defining Constants Syntaxconst data_type variable_name = value; Example:
What is the name that represents a value that does not change during program execution?A constant is a data item whose value cannot change during the program's execution. Thus, as its name implies – the value is constant.
Which means changing during the execution of the program?Answer: Values that changes during program execution is called - Variable.
Is a name which is associated with a value that can be changed?A variable is a symbolic name for (or reference to) information. The variable's name represents what information the variable contains. They are called variables because the represented information can change but the operations on the variable remain the same.
Is used to represent values that may be changed in program?In programming, a variable is a value that can change, depending on conditions or on information passed to the program. Typically, a program consists of instruction s that tell the computer what to do and data that the program uses when it is running.
|