VBScript - The Easy Stuff - Page 2

vbscript

 

VBScript - The Easy Stuff - Page 2

Chris Assenza

February 5, 2001

Variables
I think everyone knows what a variable is, but to be safe, a variable is a little chunk of memory in which a value of varying type (like characters and numbers) can be stored and manipulated. That is not the most precise or technical definition that could be used, but I suspect everyone gets the picture. Variables are what you use to store the different values floating around in your program. As I hinted at rather blatantly above, variables can take on many different forms. They can be strings (a string is one or more characters — like a sentence for example), integer, floating point number, a date, and all sorts of other useful things.

One advantage to a scripting language is that it is not strongly typed. Or, in other words, to use a variable it does not need to be declared with a type defined. Whereas in a strongly-typed language like C++, you need to declare not only the variable but what type of variable it is.

In VBScript there is really only one type, Variant. Therefore, all VBScript variables are of type Variant (which means they can basically be anything) and may be any of the following sub-types:

SUBTYPE DESCRIPTION
Empty Variant is uninitialized. Value is 0 for numeric variables or a zero-length string ("") for string variables.
Null Variant intentionally contains no valid data.  
Boolean Contains either True or False.
Byte Contains integer in the range 0 to 255.  
Integer Contains integer in the range -32,768 to 32,767.  
Currency -922,337,203,685,477.5808 to 922,337,203,685,477.5807.
Long Contains integer in the range -2,147,483,648 to 2,147,483,647.
Single Contains a single-precision, floating-point number in the range -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values.
Double Contains a double-precision, floating-point number in the range -1.79769313486232E308 to -4.94065645841247E- 324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values.
Date (Time) Contains a number that represents a date between January 1, 100 to December 31, 9999.
String Contains a variable-length string that can be up to approximately 2 billion characters in length.
Object Contains an object.
Error Contains an error number.
Source: Microsoft Scripting Technologies

The neat thing about the Variant thing is that you do not really need to worry about type at all. It will figure out what sub-type the variable is, based on how you use it. What is important to recognize is that if you utilize a variable as one sub-type (say as an integer) and then try to store a string in it later, that will return an error because of a mismatch.

To use a variable in code, it first has to be declared — or memory space has to be set aside for it. VBScript uses the "Dim" statement to declare variables (there are others but we do not need to worry about those right now). Here is an example of using the Dim statement.

  <%
    ' This little single apostrophe is a comment,
    ' it allows you to put explanation in your
    ' code and not have the interpreter run it!

    ' Use this as notes to yourself for what code
    ' does or notes for future developers working
    ' on your page!

    'Using the Dim Statement

    Dim myVariable
    Dim anotherVariable, X, Y, count
  %>

The above code is very simplistic. The first Dim statement sets up a variable called myVariable. The second line declares anotherVariable, X and Y all at basically the same time. It is there to simply demonstrate that you can declare more then one variable per line, as long as you separate each variable with a comma.

Now before you get all excited and start declaring variables until your heart is content, you should know that there are a few restrictions for naming variables. They must begin with an alphabetic character (i.e. they cannot start with a number), they cannot contain an embedded period (ie. no my.Variable type naming is acceptable), they cannot exceed 255 characters, and they must be uniquely named (i.e. cannot have two different variables named jim in the same function or sub).

 

时间: 2024-10-30 23:18:17

VBScript - The Easy Stuff - Page 2的相关文章

JavaServer Page(JSP)简介1

js|server JavaServer Page(JSP)是一种基于Java的技术,用于生成动态网页,支持跨平台.跨Web服务器. JSP可以与Microsoft的Active Server Page相比,但它使用类似HTML的标记以及Java代码片段来代替VBScript.如果你使用本身不支持ASP的Web服务器(即Apache或Netscape服务器),那么可以考虑JSP.你也可以为这些服务器购买ASP插入模块,但是很贵.而且,现在Sun还不对使用JSP收取许可费(尽管将来有可能会收的).

澄清概念:区分 JavaScript、Jscript、VBScript、ASP

javascript|js|jscript|vbscript|概念 很多人都被这几个名词搞糊涂了,这里做个归纳,彻底澄清这几种概念. JavaScript JavaScript 最先由 Netscape 公司开发,原名叫 LiveScript,设计该语言时部分参考了Java,并且当时Java语言火热得很,因此不久就改名为JavaScript.Netscape 公司在其网景浏览器上实现了该语言,应用于客户端开发. Jscript 这里不得不先提一下微软的一款产品VBScript,其语法怪异(对于J

ASP VBScript 分页函数

vbscript|分页|函数 作用: 为了通用性考虑, 本函数主要功能为分页导航, 定义数据库连接和调用数据在调用页操作. 1.1 函数 fPageCount 内容1.2 包含文件 abbr.asp, 非必须2.1 使用函数例12.2 使用函数例22.3 使用函数例3 注: 例中的脚本为跳转框必须 效果: -------------------------------------------------------------首页 上十 上一 1 2 3 4 5 6 7 8 9 10  下一 下

基础知识:JavaScript、Jscript、VBScript、ASP的区别

javascript|js|jscript|vbscript|区别 很多人都被这几个名词搞糊涂了,这里做个归纳,彻底澄清这几种概念. JavaScript JavaScript 最先由 Netscape 公司开发,原名叫 LiveScript,设计该语言时部分参考了Java,并且当时Java语言火热得很,因此不久就改名为JavaScript.Netscape 公司在其网景浏览器上实现了该语言,应用于客户端开发. Jscript 这里不得不先提一下微软的一款产品VBScript,其语法怪异(对于J

Viewing The Event Logs Remotely Via an ASP Page with WMI

If you administrate a web server on a remote machine, then you know how important it can be to be able to quickly view your event logs and "check on things" Until recently, the only way to do this was to log onto the machine via Terminal Service

Active Server Page (ASP) 常見問題

active|server Active Server Page (ASP) 常見問題 [「Active Server Page」以下簡稱「ASP」] 問: 如何將認證使用者儲存到Session變數(New) 問: ServerVariables("LOGON_USER")傳回空值(New) 問: ServerVariables("Remote_Host")傳回IP而不是HostName(New) 問: CDO版本摘要介紹 問: 何謂ADSI? 問: 何謂ADsPa

如何在 Active Server Page 中访问 SQL Server

active|server|访问 SQL Server适用于 概要本文介绍了如何使用"ActiveX 数据对象"(ADO) 在 ASP 脚本中建立到 SQL Server 的连接,同时利用 ODBC 3.0 的连接池功能. 更多信息连接池启用 ODBC 连接池. 有关连接池的其他信息以及有关如何启用连接池功能的说明,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章: 164221 如何在 ODBC 应用程序中启用连接池 ODBC DSN使用"ODBC 管理

用asp Print this Page

东子 于 00-9-3 上午 01:41:46 发表在:ASP地带 用此引用该程序<A HREF="/ASPScripts/PrintPage.asp?REF=URL_of_Web_Page"> 打印</A> PrintPage.asp 原程序<%@ Language=VBScript %><% Option Explicit %><HTML><HEAD><TITLE>4GuysFromRolla.co

有空的时候看看,:)ASP.NET Page Templates

asp.net ASP.NET Page TemplatesBy Mike Borromeo What?A website templating system is a small collection of page templates that contain and control any non page specific (redundant) code and HTML. Who?This applies to anyone looking for a flexible method