• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

VBScript - The Easy Stuff - Page 2

发布: 2007-6-30 18:56 | 作者: admin | 来源: | 查看: 15次 | 进入软件测试论坛讨论

领测软件测试网  

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).

 

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网