- VBScript 教程
- VBScript - 主頁
- VBScript - 概述
- VBScript - 語法
- VBScript - 啟用
- VBScript - 位置
- VBScript - 變數
- VBScript - 常數
- VBScript - 運算子
- VBScript - 決策
- VBScript - 迴圈
- VBScript - 事件
- VBScript - Cookie
- VBScript - 數字
- VBScript - 字串
- VBScript - 陣列
- VBScript - 日期
- VBScript 高階
- VBScript - 過程
- VBScript - 對話方塊
- VBScript - 面向物件
- VBScript - 正則表示式
- VBScript - 錯誤處理
- VBScript - 其它語句
- VBScript 有用資源
- VBScript - 問題與解答
- VBScript - 快速指南
- VBScript - 有用資源
- VBScript - 討論
VBScript - 常數
常量是一個已命名的記憶體位置,用於儲存一個在指令碼執行期間不能被改變的值。如果使用者嘗試改變一個常數值,指令碼執行將終止並出現錯誤。常量的宣告方式與變數的宣告方式相同。
宣告常量
語法
[Public | Private] Const Constant_Name = Value
常量可以是公共的(Public)或私有的(Private)。使用公共或私有是可選的。公共常量對所有指令碼和過程都可用,而私有常量只在過程或類中可用。可以將任何值(例如數字、字串或日期)賦給已宣告的常量。
示例 1
在本例中,pi 的值為 3.4,它在訊息框中顯示圓的面積。
<!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>
示例 2
以下示例說明如何將字串和日期值賦給常量。
<!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>
示例 3
在以下示例中,使用者嘗試更改常數值;因此,最終會出現一個執行錯誤。
<!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>
廣告