Literals are nice, but it isn't always clear what they mean. Encountering the number 12 in a formula, for instance, might cause the formula to generate correct results, but it would still be helpful to know what 12 means. Is it the number of months in a year, hours in a day, minimum number of teeth in a mouth to eat steak, or something even more sinister?
Constants provide a way to assign meaningful names to literal values. They are treated a lot like literal values, but once defined, they can be used over and over again in your code. Each use of a literal value, even if it has the same value, represents a distinct definition and instance of that value.
In Visual Basic, constants are defined using the Const keyword.
Const MonthsInYear As Short = 12
This constant definition has the following parts.
A name. In this case, the name is MonthsInYear.
A data type. This example defines a Short constant. The data type always follows the As keyword. If you leave out this As clause, the constant's data type will be whatever the assigned literal would have been on its own. Only the following data types can be used for constants: Boolean, Byte, Char, Date, Decimal, Double, Integer, Long, Object, SByte, Short, Single, String, UInteger, ULong, UShort, or the name of an enumeration (discussed in the next section).
An initializer. The initializer assigned here is 12. Once assigned, this value cannot be altered while your code is running. Constants are always value types, not reference types. Initializers are usually simple literals, but you can also include simple calculations.
Const Seven As Integer = 3 + 4
An access level. The definition of MonthsInYear listed here represents the typical format of a constant definition included within a code procedure. You can also define constants outside of procedures, but still within a class or other type. When you do this, you generally add an access modifier keyword just before the Const keyword. This keyword indicates how much code will be able to use the constant. I'll describe access modifiers a little later in the section on variables. Constants defined within a procedure can only be used within that procedure.
Once you define a constant, you can use it anywhere you would use an equivalent literal.
Const GreatGreeting As String = "Hello, World!" ...Later... MsgBox(GreatGreeting)