My brain is not working this morning. I need some help accessing some members from a static method. Here is a sample code, how can I modify this so that TestMethod() has access to testInt
public class TestPage
{
protected int testInt { get; set; }
protected void BuildSomething
{
// Can access here
}
[ScriptMethod, WebMethod]
public static void TestMethod()
{
// I am accessing this method from a PageMethod call on the clientside
// No access here
}
}
Best Answer
testInt
is declared as an instance field. It is impossible for astatic
method to access an instance field without having a reference to an instance of the defining class. Thus, either declaretestInt
as static, or changeTestMethod
to accept an instance ofTestPage
. Sois okay as is
Which of these is right depends very much on what you're trying to model. If
testInt
represents state of an instance ofTestPage
then use the latter. IftestInt
is something about the typeTestPage
then use the former.