VBScript - 变量


VBScript 变量

变量是一个命名的内存位置,用于保存在脚本执行期间可以更改的值。VBScript 只有一种基本数据类型,即Variant

声明变量的规则 -

  • 变量名称必须以字母开头。

  • 变量名称不能超过 255 个字符。

  • 变量不应包含句点 (.)

  • 变量名在声明的上下文中应该是唯一的。

声明变量

变量使用“dim”关键字声明。由于只有一种基本数据类型,因此默认情况下所有声明的变量都是变体。因此,用户无需在声明期间提及数据类型。

示例 1 - 在此示例中,IntValue 可以用作字符串、整数甚至数组。

Dim Var

示例 2 - 两个或多个声明用逗号 (,) 分隔

Dim Variable1,Variable2

为变量赋值

值的分配类似于代数表达式。左侧是变量名称,后跟等于 (=) 符号,右侧是其值。

规则

  • 声明数值时应不带双引号。

  • 字符串值应用双引号 (") 括起来

  • 日期和时间变量应包含在井号 (#) 内

例子

' Below Example, The value 25 is assigned to the variable.
Value1 = 25

' A String Value ‘VBScript’ is assigned to the variable StrValue.
StrValue = “VBScript”

' The date 01/01/2020 is assigned to the variable DToday.
Date1 = #01/01/2020#

' A Specific Time Stamp is assigned to a variable in the below example.
Time1 = #12:30:44 PM#

变量的范围

可以使用以下确定变量范围的语句来声明变量。在过程或类中使用时,变量的范围起着至关重要的作用。

  • 暗淡
  • 民众
  • 私人的

暗淡

在过程级别使用“Dim”关键字声明的变量仅在同一过程中可用。在脚本级别使用“Dim”关键字声明的变量可用于同一脚本中的所有过程。

示例- 在下面的示例中,Var1 和 Var2 的值在脚本级别声明,而 Var3 在过程级别声明。

注意- 本章的范围是理解变量。函数将在接下来的章节中详细讨论。

<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         Dim Var1
         Dim Var2
         
         Call add()
         Function add()
            Var1 = 10
            Var2 = 15
            Dim Var3
            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   ' Var3 has No Scope outside the procedure. Prints Empty
      </script>
   </body>
</html>

民众

使用“Public”关键字声明的变量可用于所有关联脚本中的所有过程。当声明“public”类型的变量时,Dim 关键字被“Public”替换。

示例- 在以下示例中,Var1 和 Var2 在脚本级别可用,而 Var3 在关联的脚本和过程中可用,因为它被声明为公共。

<!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>

私人的

声明为“私有”的变量仅在声明它们的脚本内具有作用域。当声明“Private”类型的变量时,Dim 关键字被“Private”替换。

示例- 在以下示例中,Var1 和 Var2 在脚本级别可用。Var3 被声明为 Private,并且仅适用于此特定脚本。“私有”变量的使用在类中更为明显。

<!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>