VBScript Constants

Advertisements


VBScript Constants

Constant is a named memory location used to hold a value that CANNOT be changed during the script execution. If a user tries to change a Constant Value, the Script execution ends up with an error. Constants are declared the same way the variables are declared.

Declaring Constants

Syntax:

[Public | Private] Const Constant_Name = Value

The Constant can be of type Public or Private. The Use of Public or Private is Optional. The Public constants are available for all the scripts and procedures while the Private Constants are available within the procedure or Class. One can assign any value such as number, String or Date to the declared Constant.

Example 1:

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 2:

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 3:

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>


Advertisements
Advertisements