📄 vbscript_variables.asp
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>VBScript Variables</title>
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />
<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<link rel="stylesheet" type="text/css" href="../stdtheme.css" />
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</head>
<body>
<a name="top"></a>
<table cellpadding="0" cellspacing="0" width="100%" bgcolor="#808080">
<tr>
<td width="234" valign="top">
<a href="../default.htm"><img src="../images/w3default80.jpg" border="0" alt="W3Schools" /></a>
</td>
<th valign="middle" align="left" class="right">
<iframe style="background-color:#808080" src="../banners/bannerframe.asp@adpartner=w3markup" height="90" width="728"
marginwidth="0" marginheight="0" frameborder="0" scrolling="no">
Your browser does not support inline frames or is currently configured not to display inline frames.
</iframe>
<br /></th>
</tr>
</table>
<table style="margin-left:1px" width="800px" border="0" cellpadding="0" cellspacing="2">
<tr>
<td width="137" class="content" valign="top"><br />
<a class="left" href="../default.asp" target="_top">
<img src="../images/homeicon.gif" border="0" alt="home" /></a>
<a class="left" href="../default.asp" target="_top">
<b>HOME</b></a>
<br /><br />
<b>VBScript Tutorial</b><br />
<a class="left" target="_top" href="default.asp" >VB HOME</a><br />
<a class="left" target="_top" href="vbscript_intro.asp" >VB Introduction</a><br />
<a class="left" target="_top" href="vbscript_howto.asp" >VB How to</a><br />
<a class="left" target="_top" href="vbscript_whereto.asp" >VB Where to</a><br />
<a class="left" target="_top" href="vbscript_variables.asp" style='font-weight:bold;'>VB Variables</a><br />
<a class="left" target="_top" href="vbscript_procedures.asp" >VB Procedures</a><br />
<a class="left" target="_top" href="vbscript_conditionals.asp" >VB Conditional</a><br />
<a class="left" target="_top" href="vbscript_looping.asp" >VB Looping</a><br />
<a class="left" target="_top" href="vbscript_summary.asp" >VB Summary</a><br />
<br />
<b>Examples</b><br />
<a class="left" target="_top" href="vbscript_examples.asp" >VB Examples</a><br />
<br />
<b>References</b><br />
<a class="left" target="_top" href="vbscript_ref_functions.asp" >VB Functions</a><br />
<a class="left" target="_top" href="vbscript_ref_keywords.asp" >VB Keywords</a><br />
<br />
<b>Selected Reading</b><br />
<a class="left" target="_top" href="../browsers/browsers_stats.asp">Web Statistics</a><br />
<a class="left" target="_top" href="../site/site_glossary.asp">Web Glossary</a><br />
<a class="left" target="_top" href="../hosting/default.asp">Web Hosting</a><br />
<a class="left" target="_top" href="../quality/default.asp">Web Quality</a><br /><br />
<a class="left" target="_top" href="../forum/default.asp">W3Schools Forum</a><br /><br />
<a class="left" target="_top" href="../about/about_helping.asp">Helping W3Schools</a><br /><br />
<script type="text/javascript"><!--
google_ad_client = "pub-3440800076797949";
/*LeftLinkUnit*/
google_ad_slot = "4854527104";
google_ad_width = 120;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="../../pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</td>
<td valign="top">
<table style="border: 1px solid gray" width="100%" bgcolor="#FFFFFF" border="0" cellpadding="5" cellspacing="0">
<tr>
<td>
<h1>VBScript Variables</h1>
<a href="vbscript_whereto.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="vbscript_procedures.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<hr />
<h2>Examples</h2>
<p>
<a target="_blank" href="tryit.asp@filename=vbdemo_variable">Create a variable</a><br />
Variables are used to store information. This example demonstrates how you can
create a variable, and assign a value to it.</p>
<p>
<a target="_blank" href="tryit.asp@filename=vbdemo_variable2">Insert a variable value in a text</a><br />
This example demonstrates how you can insert a variable value in a text.</p>
<p>
<a target="_blank" href="tryit.asp@filename=vbdemo_array">Create an array</a><br />
Arrays are used to store a series of related data items. This example demonstrates how you can make an array that stores
names. ( We are using a "for loop" to demonstrate how you write the
names. )</p>
<hr />
<h2>What is a Variable?</h2>
<p>A variable is a "container" for information you want to store. A
variable's value can change during the script.
You can refer to a variable by name to see its value or to change its value. In VBScript,
all variables are of type <i>variant</i>, that can store different types of
data. </p>
<hr />
<h2>Rules for Variable Names:</h2>
<ul>
<li>Must begin with a letter </li>
<li>Cannot contain a period (.)</li>
<li>Cannot exceed 255 characters</li>
</ul>
<hr />
<h2>Declaring Variables</h2>
<p>
You can declare variables with the Dim, Public or the Private statement. Like this:
</p>
<table width="100%" border="1" class="ex" cellspacing="0"><tr><td>
<pre>dim name
name=some value</pre>
</td></tr></table>
<p>
Now you have created a variable. The name of the variable is "name".
</p>
<p>You can also declare variables by using its name in your script. Like this:</p>
<table width="100%" border="1" class="ex" cellspacing="0"><tr><td>
<pre>name=some value</pre>
</td></tr></table>
<p>
Now you have also created a variable. The name of the variable is "name".
</p>
<p> However, the last method is not a good practice, because you can misspell the variable name later
in your script, and that can cause strange results when your script is running.
This is because when you misspell for example the "name" variable to
"nime" the script will automatically create a new variable called
"nime". To prevent your script from doing this you can use the Option Explicit statement.
When you use this statement you will have to declare all your variables with the
dim, public or private statement. Put the Option Explicit statement on the top of your script. Like
this:
</p>
<table width="100%" border="1" class="ex" cellspacing="0"><tr><td>
<pre>option explicit
dim name
name=some value</pre>
</td></tr></table>
<br />
<hr />
<h2>Assigning Values to Variables</h2>
<p>You assign a value to a variable like this:
</p>
<table width="100%" border="1" class="ex" cellspacing="0"><tr><td>
<pre>name="Hege"
i=200</pre>
</td></tr></table>
<p>The variable name is on the left side of the expression and the value you want to
assign to the variable is on the right. Now the variable "name" has
the value "Hege".
</p>
<hr />
<h2>Lifetime of Variables</h2>
<p>How long a variable exists is its lifetime.<br />
<br />
When you declare a variable within a procedure, the variable can only be
accessed within that procedure. When the procedure exits, the variable is destroyed.
These variables are called local variables.
You can have local variables with the same name in different procedures, because
each is recognized only by the procedure in which it is declared.<br />
<br />
If you declare a variable outside a procedure, all the procedures on your page
can access it. The lifetime of these variables starts when they are declared,
and ends when the page is closed.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -