<P> when a variable is declared or created in the script body, the global scope is used by default, the default value is private scope. </P>
<P> to explicitly declare the scope of a variable, public and private statements can be used. </P>
<Div class = "cnblogs_code" style = "border-top: # cccccc 1px solid; border-Right: # cccccc 1px solid; border-bottom: # cccccc 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; border-left: # cccccc 1px solid; padding-Right: 5px; Background-color: # f5f5f5 "> <PRE> <span style =" color: # 0000ff "> Public </span> myarray (<span style =" color: #800080 "> 10 </span>) </PRE> </div>
<P> & nbsp; </P>
<P> & nbsp; </P>
<P> by default, variables can be used without prior declaration. However, this may cause a spelling error and a new variable to be created, causing a bug. </P>
<P> Option explicit statements can help prevent such bugs. </P>
<P> use the option explicit statement as the first line of the script file, and then use the dim statement to list each variable used in the script. (<Em> Dim is often used to declare arrays, but can also be used to declare variables </em>) </P>
<Div class = "cnblogs_code" style = "border-top: # cccccc 1px solid; border-Right: # cccccc 1px solid; border-bottom: # cccccc 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; border-left: # cccccc 1px solid; padding-Right: 5px; Background-color: # f5f5f5 "> <PRE> <span style =" color: # 0000ff "> Option </span> <span style =" color: # 0000ff "> explicit </span>
<Span style = "color: # 0000ff"> dim </span> <span style = "color: #000000"> tomorrow
Tomorrow </span> = <span style = "color: #800000"> "</span> <span style =" color: #800000 "> Saturday </span> <span style =" color: #800000 ">" </span> <span style = "color: #000000">
Wscript. echo </span> <span style = "color: #800000"> "</span> <span style =" color: #800000 "> tomorrow is </span> <span style =" color: #800000 ">" </span> & amp; tomottow </PRE> </div>
<P> an error message is generated when the script runs to an undefined tomottow. If the option explicit statement is not available, no error is reported, but "Tomorrow is" is output directly. </P>
VBScript learning notes-scope, explicit variable Declaration