Declaring named constants

As we have seen, you can use a constant in an expression by merely writing its literal value. (Such constants are called literals).

If you are going to use a constant in several places within a program, you can declare it, giving it a name and data type, using the Private statement (you cannot use Dim with named constants):

   Private const <constant name> as <data type> = <value>

For example:

   Private Const votingAge As Integer = 18
   Private Const homeState As String = "California"
   Private Const taxRate As Single = .22
Once declared, you can use a named constant as many times as you wish. This is handy if you decide you need to change its value at a later time. For example, if the votingAge, homeState or taxRate were to change in the above snippet, you would only have to change the declaration. Named constants also make a program more readable.

If you forget the type of a constant (or variable), place the cursor over it, and a popup box will give its data type.


Disclaimer: The views and opinions expressed on unofficial pages of California State University, Dominguez Hills faculty, staff or students are strictly those of the page authors. The content of these pages has not been reviewed or approved by California State University, Dominguez Hills.