Specifying Constant Data Type

If you don't explicitly specify a data type when you declare a constant, Visual Basic gives the constant the data type that best matches the expression assigned to the constant. You may occasionally want to override a constant's default data type to improve the efficiency or accuracy of your calculations. For example, Visual Basic treats constants containing real numbers with a fractional component as Double values by default. If you want to speed up your code execution, you could explicitly declare a constant as Single; if you want to avoid rounding errors, you could declare it as Currency. You may also need to declare a constant as a particular data type to ensure that the result of a computation has a specific data type.

You declare a data type for a constant the same way you declare a type for a variable, using the As keyword.


Const conTemp As Currency = 3.45