Creating Your Own ConstantsAlthough Visual Basic defines a large number of constants for many activities, sometimes you need to define your own constants. Constants are defined using the Const statement to give the constant a name and a value, as illustrated in the following syntax: [Public | Private] Const constantname [As constanttype] = value If you think this statement looks similar to the declaration of a variable, you're right. As with declaring a variable, you provide a name for the constant and, optionally, specify the type of data it should hold. The Const keyword at the beginning of the statement tells Visual Basic that this statement defines a constant. This keyword distinguishes the statement from one that just assigns a value to a variable. In declaring the type of a constant, you use the same intrinsic types as you do for defining variables. Finally, to define a constant, you must include the equal sign (=) and the value to be assigned. If you are defining a string constant or date constant, remember to enclose the value in either quotation marks (") or the pound sign (#), respectively. A constants scope is also important. The same rules for the scope of variables, which were discussed in the earlier section "Determining Where a Variable Can Be Used," apply to constants as well. |
|
![]() |
![]() |
|||