首页 | IT新闻 | 硬件 | 操作系统 | 开发 | 网络编程 | 数据库 | 热门框架 | 网络安全 | 组网 | 建站指南 | 网页制作 | 特效 | 实用技巧 | 服务器 | 办公 | QQ | 探索 | 社区

  • 技术部落
  • 部落首页 > 程序开发 > .NET专栏 > 正文
  • 简单数据类型(3)
      2007-2-25  来源:网络资源  编辑:Jsbulo  热度:

    变量的作用域

    在使用变量时会有几个简单的问题。变量可以在多大的范围内使用?如果创建了一个变量,页面上的其他事件和过程可以使用它吗?其他页面可以看到它吗?访问同一Web站点的其他用户能看到它吗?这是作用域的问题,作用域就是变量可以使用的范围。下面研究三类不同等级的变量:块变量、过程变量和全局变量。用最小的作用域创建变量是非常重要的。这样,在不需要某个变量时,就可以销毁它,释放它占用的内存。所以,变量的作用域越有限,程序运行得就越快。

    提示:

    在同一个作用域中,两个变量不能有相同的名称。为了安全起见,在Web站点中应避免使用重复的变量名。

    1. 块级变量

    作用域最有限的类型称为块级作用域。块级作用域指以End IfElseLoop、或Next等语句终止的范围(这些结构将在下一章详细讨论)在块级作用域内创建的变量声明只能用于块的内部。在该块结束时(例如在最后一个循环结束后),变量就被删除了。在下面的例子中,变量strBlockLevelVariable的作用域是位于IfEnd If之间的块,而且当执行流传到块之外时,就不能再引用该变量,此时lblMessage.Text什么也不包含。

      If l = 1 Then

        Dim strBlockLevelVariable As String

        strBlockLevelVariable = "Very Short Lived!"

      End If

      lblMessage.Text = strBlockLevelVariable 

    但是,如果在创建它的块内使用strBlockLevelVariable,如下所示,lblMessage就会显示出信息:

      If l = 1 Then

        Dim strBlockLevelVariable As String

        strBlockLevelVariable = "Very Short Lived!"

      lblMessage.Text = strBlockLevelVariable 

    End If

    块级变量的优点是节省了资源,因为在块外变量不需要占用资源。缺点是如果不小心,会在块内声明变量,而在块外试图访问它。鉴于此原因,许多程序员都避免声明块级变量。

    2. 过程级变量

    作用域的下一个级别是过程变量。这些变量可用于过程中的所有代码(例如前面使用过的Page_Load())。它们也可以称为局部变量,因为它们对于创建自己的子例程来说是局部的。在子例程的外部,局部变量没有值,这是因为变量的生存期在子例程结束时就终结了。

    试一试:创建过程级局部变量

    (1) ch03文件夹中创建TIO-VariableScope1.aspx文件,输入以下代码:

    <%@ Page Language="vb"%>

    <script runat="server">

    Sub Page_Load() 

      Dim strMyProcVariable = "Procedure Variable"

      If ISpostBack  Then

        Dim strMyBlockVariableUsedInside = " Block Variable Used In Block"

     

      lblMessageBlockInBlock.Text = strMyBlockVariableUsedInside

      lblMessageProcedure.Text = strMyProcVariable

      End If

    End Sub

     

    </script>

    <html>

    <head>

    <title> Variable Scope</title>

    </head>

    <body>

      <form runat="server">

         <asp:label id=" lblMessageBlockInBlock” runat="server" text="DEFAULT BlockInBlock"></ asp:label>

    <br />

         <asp:label id=" lblMessageProcedure" runat="server" text="DEFAULT Procedure"></ asp:label>

    <br />

         <asp:Button runat="server" text=”Submit”/>

    </form>

    </body>

    </html>

    (2) 在浏览器中打开该文件

    (3) 下面添加三行代码,在块中声明一个块级变量,但在块的外部使用它:

    <%@ Page Language="vb"%>

    <script runat="server">

    Sub Page_Load() 

      Dim strMyProcVariable = "Procedure Variable"

      If  ISpostBack  Then

        Dim strMyBlockVariableUsedInside = " Block Variable Used In Block"

        Dim strMyBlockVariableUsedOutside = " Block Variable Used After Block"

      lblMessageBlockInBlock.text = strMyBlockVariableUsedInside

      lblMessageProcedure.text = strMyProcVariable

    End If

    lblMessageBlockOutBlock.text = strMyBlockVariableUsedOutside

    End Sub

     

    </script>

    <html>

    <head>

    <title> Variable Scope</title>

    </head>

    <body>

      <form runat="server">

         <asp:label id=" lblMessageBlockInBlock" runat="server" text="DEFAULT BlockInBlock"></ asp:label>

    <br />

         <asp:label id=" lblMessageBlockOutBlock" runat="server" text="DEFAULT BlockOutBlock procedure"></ asp:label>

    <br />

         <asp:label id=" lblMessageProcedure" runat="server" text="DEFAULT - Procedure"></ asp:label>

    <br />

         <asp:Button runat="server" text="Submit"/>

    </form>

    </body>

    </html>