ASP.Net/Language Basics/Variable Scope — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 14:53, 26 мая 2010
Содержание
block-level variables (C#)
<source lang="csharp">
<%@ Page Language="c#" %> <script runat="server"> void Page_Load() {
if(1==1) { string strBlockLevelVariable; strBlockLevelVariable = "Very Short Lived!"; lblMessage.Text = strBlockLevelVariable; }
} </script> <html> <head> <title>Creating Variables Example</title> </head> <body>
<asp:Label runat="server" ID="lblMessage"/>
</body> </html>
</source>
Define page level variables (VB.net)
<source lang="csharp">
<%@ Page Language="VB" %> <script runat="server">
Dim myFirstNumber, mySecondNumber As Integer Sub SetVarsToInitialValues() myFirstNumber = 7 mySecondNumber = 8 Label1.Text = myFirstNumber Label2.Text = mySecondNumber End Sub Sub Button1_Click(sender As Object, e As EventArgs) SetVarsToInitialValues() Label3.Text = myFirstNumber + mySecondNumber End Sub Sub Button2_Click(sender As Object, e As EventArgs) SetVarsToInitialValues() Label3.Text = myFirstNumber - mySecondNumber End Sub
</script> <html> <head> </head> <body>
<form runat="server">
<asp:Label id="Label1" runat="server">Label</asp:Label> + <asp:Label id="Label2" runat="server">Label</asp:Label> = <asp:Label id="Label3" runat="server">Label</asp:Label>
<asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Add"></asp:Button> <asp:Button id="Button2" onclick="Button2_Click" runat="server" Text="Subtract"></asp:Button>
</form>
</body> </html>
</source>
Function Level variables (C#)
<source lang="csharp">
<%@ Page Language="C#" Debug="true" %> <script runat="server">
void Page_Load() { string strMyFuncVariable = "Function-Level Variable"; if(Page.IsPostBack) { string strMyBlockVariableUsedInside = "Block Variable Used In Block"; lblMessageBlockInBlock.Text = strMyBlockVariableUsedInside; lblMessageFunction.Text = strMyFuncVariable; } }
</script> <html> <head>
<title>Variable Scope</title>
</head> <body>
<form runat="server"> <asp:Label id="lblMessageBlockInBlock" runat="server" text="DEFAULT - BlockInBlock"></asp:Label>
<asp:Label id="lblMessageFunction" runat="server" text="DEFAULT - Function"></asp:Label>
<asp:Button id="Button1" runat="server" Text="Submit"></asp:Button> </form>
</body> </html>
</source>
Local variable inside a function (VB.net)
<source lang="csharp">
<script language="vb" runat="server"> Sub Page_Load()
Response.Write ("
Calling Subroutine 1...") Call Subroutine_1() Response.Write ("
Calling Subroutine 2...") Call Subroutine_2() Response.Write ("
Calling Subroutine 1...") Call Subroutine_1()
End Sub
Sub Subroutine_1() Dim Different As String Different = "Hello I"m the variable Different in Subroutine 1" Response.Write (Different) End Sub Sub Subroutine_2() Dim Different As String Different = "Hello I"m the variable Different in Subroutine 2" Response.Write (Different) End Sub
</script> <html> <head> <title>Scope</title> </head> <body> </body> </html>
</source>
Page level variable and function level variable (VB.net)
<source lang="csharp">
<script language="vb" runat="server">
Dim Global As String ="
Hello I"m a persistent global variable"
Sub Page_Load()
Response.Write (Global) Response.Write ("
Calling Subroutine 1...") Call Subroutine_1() Response.Write ("
Calling Subroutine 2...") Call Subroutine_2() Response.Write ("
Calling Subroutine 1...") Call Subroutine_1()
End Sub
Sub Subroutine_1() Dim Different As String Different = "Hello I"m the variable Different in Subroutine 1" Response.Write (Different) Response.Write (Global) End Sub Sub Subroutine_2() Dim Different As String Different = "Hello I"m the variable Different in Subroutine 2" Response.Write (Different) Response.Write (Global) End Sub
</script> <html> <head> <title>Scope</title> </head> <body> </body> </html>
</source>
Page Level Variable (C#)
<source lang="csharp">
<%@ Page Language="C#" Debug="true" %> <script runat="server">
string strVariableGlobal = "Variable Global: Use me anyplace on the page"; void Page_Load() { string strMyVariable1 = "Block Variable Used In Block"; if (1==1) { string strMyVariable2 = "Block Variable Used In Block"; string strMyVariable3 = "Block Variable Used After Block"; lblMessageBlockInBlock.Text = strVariableGlobal; } lblMessageProcedure.Text = strVariableGlobal; SubProcedure2(); } void SubProcedure2() { lblMessageBlockOutBlock.Text = strVariableGlobal; }
</script> <html> <head>
<title>Variable Scope</title>
</head> <body>
<form runat="server"> <asp:Label id="lblMessageBlockInBlock" runat="server" text="DEFAULT - BlockInBlock"/>
<asp:Label id="lblMessageBlockOutBlock" runat="server" text="DEFAULT - BlockOutBlock"></asp:Label>
<asp:Label id="lblMessageProcedure" runat="server" text="DEFAULT - Procedure"></asp:Label>
</form>
</body> </html>
</source>