C# 在c中使局部变量值成为全局变量#
我有以下代码。在这段代码中,我可以通过使用eventHandling获得字符串值,如1、2、3等。如何获得值目前并不重要。我现在需要的是能够在页面加载事件之外访问此字符串值,如下面给出的函数C# 在c中使局部变量值成为全局变量#,c#,asp.net,variables,global-variables,C#,Asp.net,Variables,Global Variables,我有以下代码。在这段代码中,我可以通过使用eventHandling获得字符串值,如1、2、3等。如何获得值目前并不重要。我现在需要的是能够在页面加载事件之外访问此字符串值,如下面给出的函数myfun()中。我如何实现这一点 protected void Page_Load(object sender, EventArgs e) { hfm mymaster = (hfm)Page.Master; lcont lc = mymaster.getlcont(); lc.m
myfun()
中。我如何实现这一点
protected void Page_Load(object sender, EventArgs e)
{
hfm mymaster = (hfm)Page.Master;
lcont lc = mymaster.getlcont();
lc.myevent += delegate(string st)
{
//slbl.Text = st;
string str =st;
}
}
protectd void myfun()
{
//i want to access the string value "st" here.
}
根据我的经验,您只需在函数范围之外声明您想要全局的变量 IE:不管/无论它们在哪里
string st; // St is declared outside of their scopes
protected void Page_Load(object sender, EventArgs e)
{}
protectd void myfun()
{
}
根据我的经验,您只需在函数范围之外声明您想要全局的变量 IE:不管/无论它们在哪里
string st; // St is declared outside of their scopes
protected void Page_Load(object sender, EventArgs e)
{}
protectd void myfun()
{
}
您可以将其公开: 公共-可以从任何地方联系到该成员。这是限制最少的可见性。默认情况下,枚举和接口是公共可见的 示例
<visibility> <data type> <name> = <value>;
您可以将其公开: 公共-可以从任何地方联系到该成员。这是限制最少的可见性。默认情况下,枚举和接口是公共可见的 示例
<visibility> <data type> <name> = <value>;
将全局(或类?)变量放在页面加载之前或类声明之后
public partial class Index : System.Web.UI.Page
{
private string str = "";
protected void Page_Load(object sender, EventArgs e)
{
hfm mymaster = (hfm)Page.Master;
lcont lc = mymaster.getlcont();
lc.myevent += delegate(string st)
{
//slbl.Text = st;
str =st;
}
}
protectd void myfun()
{
//i want to access the string value "st" here.
//value of st has been passed to str already in page_load.
string newString = str;
}
}
将全局(或类?)变量放在页面加载之前或类声明之后
public partial class Index : System.Web.UI.Page
{
private string str = "";
protected void Page_Load(object sender, EventArgs e)
{
hfm mymaster = (hfm)Page.Master;
lcont lc = mymaster.getlcont();
lc.myevent += delegate(string st)
{
//slbl.Text = st;
str =st;
}
}
protectd void myfun()
{
//i want to access the string value "st" here.
//value of st has been passed to str already in page_load.
string newString = str;
}
}
一个简单的改变就能使之成为可能。将str声明为全局变量
public class Form1
{
string str = "";//Globel declaration of variable
protected void Page_Load(object sender, EventArgs e)
{
}
}
一个简单的改变就能使之成为可能。将str声明为全局变量
public class Form1
{
string str = "";//Globel declaration of variable
protected void Page_Load(object sender, EventArgs e)
{
}
}
如我所见,您可以通过两种方式完成: 1) 作为参数传递:
protected void Page_Load(object sender, EventArgs e)
{
hfm mymaster = (hfm)Page.Master;
lcont lc = mymaster.getlcont();
lc.myevent += delegate(string st)
{
//slbl.Text = st;
string str =st;
myfunc(str); // pass as param
}
}
protectd void myfun(string str) // see signature
{
//i want to access the string value "st" here.
}
2) 创建一个类变量:
string classvariable;
protected void Page_Load(object sender, EventArgs e)
{
hfm mymaster = (hfm)Page.Master;
lcont lc = mymaster.getlcont();
lc.myevent += delegate(string st)
{
//slbl.Text = st;
string str =st;
classvariable = str; // set it here
}
}
protectd void myfun()
{
//i want to access the string value "st" here. // get it here
}
如我所见,您可以通过两种方式完成: 1) 作为参数传递:
protected void Page_Load(object sender, EventArgs e)
{
hfm mymaster = (hfm)Page.Master;
lcont lc = mymaster.getlcont();
lc.myevent += delegate(string st)
{
//slbl.Text = st;
string str =st;
myfunc(str); // pass as param
}
}
protectd void myfun(string str) // see signature
{
//i want to access the string value "st" here.
}
2) 创建一个类变量:
string classvariable;
protected void Page_Load(object sender, EventArgs e)
{
hfm mymaster = (hfm)Page.Master;
lcont lc = mymaster.getlcont();
lc.myevent += delegate(string st)
{
//slbl.Text = st;
string str =st;
classvariable = str; // set it here
}
}
protectd void myfun()
{
//i want to access the string value "st" here. // get it here
}
在类范围而不是委托范围中定义它,或将其作为参数传递给
myfun()
在类范围而不是委托范围中定义它,或将其作为参数传递给myfun()