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
  • Public
  • Private

Dim

在過程級別使用“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”關鍵字宣告的變數可用於所有關聯指令碼中的所有過程。宣告“public”型別的變數時,“Dim”關鍵字將被“Public”替換。

**示例** - 在以下示例中,Var1 和 Var2 在指令碼級別可用,而 Var3 在關聯的指令碼和過程中可用,因為它被宣告為 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>

Private

宣告為“Private”的變數僅在其宣告的指令碼中具有作用域。宣告“Private”型別的變數時,“Dim”關鍵字將被“Private”替換。

**示例** - 在以下示例中,Var1 和 Var2 在指令碼級別可用。Var3 宣告為 Private,它僅對該特定指令碼可用。“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>
廣告