Slide show

[BasicVbscript][slideshow]

How to declare variable in vbscript


 VBScript - Variables

variables of vbscript
Variables of VBScript 

                Variables form the basis of programming.

                variable is a memory location used to store a value that can be changed during  execution of programme or script.

                 Variables are not only  store value but also store an expression. Whenever you have a multiple of data to work with, you will have to declare a variable.
                For example, 
                                if you have to store names of employee and their  salaries of employees, you will be using variables named as emp_name or emp_sal.

                Variables can also be used for store expressions. Suppose you have stored the name of student means first name and last name using the variables f_name and s_name.

    Declaring Variables

                 Before you use variable you must declare it. For declaring variables we using “ dim ” keyword. In other programming language we need to declare variable data type but in vbscript not need to declare there data type .All variables by default is variant.

Example :
                  1.In this example ,We declare stud_name to store name of student.
                                     Dim stud_name

                  2.In this example ,we declare two or more variables are separated by comma [  , ].
                                  Dim var1,var2,var3

          For declaring any variables you should fallow  :
                 1.Variables name must be unique.
                 2.Variables name start with letter or under score ‘ _’.
                 3.In variables name not use any special character like {[=/*]} etc.
                 4.Not use keyword or reserved word for variables.
                 5. Variable names is not  more than  255 char.

         Declaring Variables

                 Variables are declared using “dim”  reserved word or keyword.

                  There is one fundamental data type, all the declared variables by default have  variant type. Hence, a user  no need to mention the data  type of variables  during declaration.

         Example

                 Example 1:
                                  In this Example, int value can be used as a string, Integer or even arrays.
                                                   Dim val

                Example 2: 
                                  Two or more declarations of variables are separated by comma(,)
                                                 Dim emp1,emp2

           Assigning Values to the Variables :

                 The variable name on the left hand side followed by an equal to (=) symbol and then its value on the right hand side.

For assign values we use assignment operator equal to[ = ].

                 We put variables to left side of operator and right side value of variable.

Rules:

                 The number should be declared without [  “  ”]  double quotes.

                 The String values should be enclosed within [  “  ”]    double quotes.

                 symbol(#)hash  used to declare Date and Time variables enclosed .

          Examples:
                 In this example  , The value 10 is assigned to the variable.
                                  val1 = 10

                  A String Value ‘TechGuru’ is assigned to the variable str.
                                  str = “TechGuru”

                  The date 20/11/2020 is assigned to the variable Today.
                                  Today = #20/11/2020#

       Scope of the Variables :

                 Scope of the Variables means , area of variable in simple word where we use any declared variables in a programme.
                         1.Dim
                        2.Public
                        3.Private

        1.Dim :

                 Variables declared using “Dim” keyword at a function are available only within the same function. Variables declared using “Dim” Keyword at script level are available to all the function within the same script.

                 Example :
                           In this example, the value of v1 and v2 are declared at script level while v3 is declared at function level.

<!DOCTYPE html>
     <html>
         <body>
                <script language="vbscript" type="text/vbscript">

                    Dim v1
                    Dim v2

                    Call multiply ()
                    
                        Function multiply()
                           v1 = 5
                           v2 = 5
                           Dim v3
                           v3 = v1 * v2
                           Msgbox v3 'Show 25, the multiplication of two values.
                        End Function

                   Msgbox v1   ' show 5 as v1 is declared at Script level
                   Msgbox v2   ' show 5 as v2 is declared at Script level
                   Msgbox v3   ' v3 has No Scope outside the function print Empty

            </script>
        </body>
    </html>
     
        2.Public:

                 Variables declared using "Public" Keyword are available to all the function across all the associated scripts. When declaring a variable of type "public", Dim keyword is replaced by "Public".

                 Example : 
                                  In the below example, Var1 and Var2 are available at script level while Var3 is available across the associated scripts and procedures as it is declared as Public.

<!DOCTYPE html>
     <html>
          <body>
               <script language="vbscript" type="text/vbscript">
                    Dim Var1
                    Dim Var2

                         Public Var3

                         Call add()

                              Function add()
                                 Var1 = 10
                                 Var2 = 15
                                 Var3 = Var1+Var2
                                 Msgbox Var3 'Displays 25, the sum of two values.
                             End Function

                    Msgbox Var1   ' Displays 10 as Var1 is declared at Script level
                    Msgbox Var2   ' Displays 15 as Var2 is declared at Script level
                    Msgbox Var3   ' Displays 25 as Var3 is declared as Public
           
              </script>
         </body>
</html>
        3.Private

                 Variables that are declared as "Private" have scope only within that script in which they are declared. When declaring a variable of type "Private", Dim keyword is replaced by "Private".

                 Example :
                                   In the below example, Var1 and Var2 are available at Script Level. Var3 is declared as Private and it is available only for this particular script. Use of "Private" Variables is more pronounced within the Class.

<!DOCTYPE html>
     <html>
          <body>
               <script language="vbscript" type="text/vbscript">
                    Dim Var1
                    Dim Var2
                    Private Var3

                    Call add()
                         Function add()
                            Var1 = 10
                            Var2 = 15
                            Var3 = Var1+Var2
                            Msgbox Var3 'Displays the sum of two values.
                         End Function

               Msgbox Var1   ' Displays 10 as Var1 is declared at Script level
               Msgbox Var2   ' Displays 15 as Var2 is declared at Script level
               Msgbox Var3   ' Displays 25 but Var3 is available only for this script.

          </script>
     </body>
</html>












No comments:

Basic Vbscript

[BasicVbscript][stack]

Advanced Vbscript

[BasicVbscript][grids]

Example

[BasicVbscript][btop]