Constants

A constant is a defined location used to store a value that must not be changed during execution.

An error will occur if a script attempts to change a constant value.

Declaring Constants

Constants are declared the same way as variables.

Syntax

[Public | Private] Const Constant_Name = Value

It is optional to use public or private constants. Public constants are available for all scripts and procedures. Private constants are only available within the procedure or class.

Any type of value can be assigned to a declared Constant.

Example

In this example, the value of pi is 3.4 and it displays the area of the circle in a message box.

<!DOCTYPE html>

<html>

<body>

<script language="vbscript" type="text/vbscript">  

  Dim intRadius   intRadius = 20   const pi=3.14

  Area = pi*intRadius*intRadius

  Msgbox Area  

</script>

</body>

</html>

Example

The below example illustrates how to assign a String and Date Value to a Constant.

<!DOCTYPE html>

<html>

<body>

<script language="vbscript" type="text/vbscript">

  Const myString = "VBScript"

  Const myDate = #01/01/2050#

  Msgbox myString

  Msgbox myDate

</script>

</body>

</html>

Example

In the below example, the user tries to change the Constant Value; hence, it will end up with an Execution Error.

<!DOCTYPE html>

<html>

<body>

<script language="vbscript" type="text/vbscript">

   Dim intRadius    intRadius = 20    const pi=3.14    pi = pi*pi 'pi VALUE CANNOT BE CHANGED.THROWS ERROR'

   Area = pi*intRadius*intRadius

   Msgbox Area

</script>

</body>

</html>