I have the following code.In this code i am able to get the string value like 1,2,3 etc through the use of eventHandling.How i get the value is not important for now.What i need now is to be able to access this string value outside the page_load event like in the function myfun()
as given below.How do i acheive that.
我有以下代码。在这段代码中,我能够通过使用eventHandling得到字符串值1,2,3等。我得到的值现在不重要。我现在需要的是能够在page_load事件之外访问此字符串值,如下面给出的函数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.
}
5 个解决方案
#1
1
You can do it in two ways as i see:
您可以通过以下两种方式完成此操作:
1) Pass as param:
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) Make a class variable:
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
}
#2
1
In my experience, you would simply declare the variable you want global outside of the scope of the functions.
根据我的经验,您只需在函数范围之外声明您想要的全局变量。
IE: Whatever / wherever they are contained.
IE:无论在何处/何处包含它们。
string st; // St is declared outside of their scopes
protected void Page_Load(object sender, EventArgs e)
{}
protectd void myfun()
{
}
#3
1
You can make it public:
你可以把它公之于众:
public - the member can be reached from anywhere. This is the least restrictive visibility. Enums and interfaces are, by default, publicly visible
公众 - 可以从任何地方联系到会员。这是限制性最小的可见性。默认情况下,枚举和界面是公开可见的
Example
<visibility> <data type> <name> = <value>;
or
public string name = "John Doe";
#4
1
Place your global (or class?) variable before the Page_Load or right after the Class declaration.
将您的全局(或类?)变量放在Page_Load之前或者在Class声明之后。
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;
}
}
#5
1
A single change can make it possible. declare str as global variable
一次改变就可以实现。将str声明为全局变量
public class Form1
{
string str = "";//Globel declaration of variable
protected void Page_Load(object sender, EventArgs e)
{
}
}
#1
1
You can do it in two ways as i see:
您可以通过以下两种方式完成此操作:
1) Pass as param:
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) Make a class variable:
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
}
#2
1
In my experience, you would simply declare the variable you want global outside of the scope of the functions.
根据我的经验,您只需在函数范围之外声明您想要的全局变量。
IE: Whatever / wherever they are contained.
IE:无论在何处/何处包含它们。
string st; // St is declared outside of their scopes
protected void Page_Load(object sender, EventArgs e)
{}
protectd void myfun()
{
}
#3
1
You can make it public:
你可以把它公之于众:
public - the member can be reached from anywhere. This is the least restrictive visibility. Enums and interfaces are, by default, publicly visible
公众 - 可以从任何地方联系到会员。这是限制性最小的可见性。默认情况下,枚举和界面是公开可见的
Example
<visibility> <data type> <name> = <value>;
or
public string name = "John Doe";
#4
1
Place your global (or class?) variable before the Page_Load or right after the Class declaration.
将您的全局(或类?)变量放在Page_Load之前或者在Class声明之后。
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;
}
}
#5
1
A single change can make it possible. declare str as global variable
一次改变就可以实现。将str声明为全局变量
public class Form1
{
string str = "";//Globel declaration of variable
protected void Page_Load(object sender, EventArgs e)
{
}
}