I have a code behind page that has several method; one of them is a page method.
[WebMethod]
public static void ResetDate(DateTime TheNewDate)
{
LoadCallHistory(TheNewDate.Date);
}
protected void LoadCallHistory(DateTime TheDate)
{ bunch of stuff }
The method LoadCallHistory works fine when the page loads and I can call it from other methods inside the page. However, in the web method part, it gets underlined in red with the error “an object reference is required for the non-static field”.
How do you access functions from the page method part of the code?
Thanks.
You cannot call a non-static method from a static context without having an instance of the class. Either remove
staticfromResetDateor makeLoadCallHistorystatic.However, if you remove
staticfromResetDateyou must have an instance of it to use that method. Another approach is to create an instance of the class insideResetDateand use that instance to callLoadCallHistory, something like this:The error message indicates that
ResetDatehas the keywordstaticandLoadCallHistorydoes not. When using static either both of the methods needs to be static or the called method needs to bestatic, the caller cannot be static if the called method is not.To quote MSDN on “Static Classes and Static Class Members”