Slide show

[BasicVbscript][slideshow]

How to define constant in vbscript

 

VBScript – Constants






Like variable we use constant to store a value but that cannot be changed during any vbscript execution .If any user try to change any value of constant ,then script stop there execution with an error.

Like variable declaration we declare constant value.

Syntax:

[Private | Public] Const Constant_Name = Value

 

The constant use two access specifiers i.e. Private and Public .But use of private or public is optional .

When we use public constant then that constant value can be access by all over the script and private constant that constant value is only access by function or procedure.

Example 1

In this example, We calculate 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

In the below example explain how to assign a Date and String Value to a Constant.

 <!DOCTYPE html>
   <html>
    <body>
      <script language = "vbscript" type = "text/vbscript">
         Const str = "Hello Public"
         Const date = #02/02/2021#
         Msgbox str
         Msgbox date
       </script>
    </body>

  </html>

Example 3

In the below example, the user try to change the Value  of Constant then stop the execution of script .

<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         Dim radius
         radius = 11
         const pi = 3.14
         pi = pi*pi '
         Area = pi* radius * radius
         Msgbox Area
         
      </script>
   </body>

</html>

 





No comments:

Basic Vbscript

[BasicVbscript][stack]

Advanced Vbscript

[BasicVbscript][grids]

Example

[BasicVbscript][btop]