English 中文(简体)
VBScript - Constants
  • 时间:2024-12-22

VBScript - Constants


Previous Page Next Page  

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

[Pubpc | Private] Const Constant_Name = Value

The Constant can be of type Pubpc or Private. The Use of Pubpc or Private is Optional. The Pubpc 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