- 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>
广告