I have a simple asp.net webform page which i use to display page related data based on pageID.
I am trying to define page level variable so that i can access there variable in different function used in this page. But i am getting few error. for example if my code is like
public partial class PageHome : System.Web.UI.Page
{
int _LangID = 1;
_LangID = int.Parse(Helper.GetAppSetting("LangID_En"));
int _PageID = 0;
int _PID = Helper.GetPageID(_LangID, "Home.aspx");
protected void Page_Load(object sender, EventArgs e)
{
int _LanguageID = _LangID;
GetPageDetails(_PageID);
}
}
Then i get
Error message:
Invalid token '=' in class, struct, or interface member declaration (_LangID = int.Parse(Helper.GetAppSetting("LangID_En"));)
Invalid token '.' in class, struct, or interface member declaration (_LangID = int.Parse(Helper.GetAppSetting("LangID_En"));)
Method must have a return type
Identifier expected
and if i try to use define these variable inside constructor
//public PageHome()
//{
//int _LangID = 1;
//_LangID = int.Parse(Helper.GetAppSetting("LangID_En"));
//int _PageID = 0;
//int _PID = Helper.GetPageID(_LangID, "Home.aspx");
//}
Then I get different errors like
The name '_LangID' does not exist in the current context
The name '_PageID ' does not exist in the current context
What is the best way to define page level variable which are defined once and access in different function
Update:
Helper.GetAppSetting("LangID_En")
and Helper.GetPageID(_LangID, "Home.aspx")
functions are defined in a seperate class file under kept under App_Code/Helper/Helper.cs
Update Two: Working code based on JonSKeet
answer.
public partial class PageHome : System.Web.UI.Page
{
int _LangID = 1;
_LangID = int.Parse(Helper.GetAppSetting("LangID_En"));
int _PageID = 0;
public PageHome()
{
int _PID = Helper.GetPageID(_LangID, "Home.aspx");
}
protected void Page_Load(object sender, EventArgs e)
{
int _LanguageID = _LangID;
GetPageDetails(_PageID);
}
}
You can't just have a statement like:
_LangID = int.Parse(Helper.GetAppSetting("LangID_En"));
within a class declaration. It would have to be in a method, or constructor, or something like that.
However, given that you've just declared the variable, you could simply change these two lines:
int _LangID = 1;
_LangID = int.Parse(Helper.GetAppSetting("LangID_En"));
... to one:
int _LangID = int.Parse(Helper.GetAppSetting("LangID_En"));
However, you'll then have an error here:
int _PID = Helper.GetPageID(_LangID, "Home.aspx");
as you can't refer to one instance field within the initializer for another. That initialization would have to happen in the constructor... in which case I'd suggest doing both of them in the constructor, for clarity:
public class PageHome : Page
{
// Do you really not need to initialize this to something else?
int _PageID = 0;
int _LangID;
int _PID;
public PageHome()
{
_LangID = int.Parse(Helper.GetAppSetting("LangID_En"));
_PID = Helper.GetPageID(_LangID, "Home.aspx");
}
...
}
On the other hand, do those values actually change on a per-page-instance basis? Perhaps they should actually be static fields? (It's hard to tell what the bigger picture is here.)
See more on this question at Stackoverflow