i have a WebMethod that serves an Ajax AutoComplete Extender as it’s DataSource.
FontnmsList_AutCpltDataSrc()
so it must be static ,… unless I will implement it through another approach… such as Web Service, as i wouldn’t like to use, and actually, that’s not the issue here anyways.
so … being a static method, and a it needs to co-work with rest of data of current application …that is not static….there’s a little problem here ..
now as i have been advised to refrain from using static in general (was referring asp.net), and while trying to follow that advice,
i could see that in order to allow interaction with it… the web method is kind of leading me to convert all of my other application elements / data types and methods to use a static modifier, for instance ,
this is an example I came across with current project.
// non static
public SeSn.CurrentSesionVariablsTmplt ExtractSesnVar()
{
SeSn.CurrentSesionVariablsTmplt RetrndAppGlobals = SeSn.GetValueAS.ACloneOfGlobalsObj("_CurrentSesionGlobals");
return (SeSn.CurrentSesionVariablsTmplt)RetrndAppGlobals ;
}
now this is the web method that is responsible for autocomplete extender
public static List<string> FntsList = new List<string>();
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod(EnableSession = true)]
public static List<string> FontnmsList_AutCpltDataSrc(string prefixText)
{
if (ExtrctSesnVar().Fntlist != null)
{
//here too , non of these are , except for the List of course
FntsList = RflectMeths.ClassFldsAsList<fntNams>();
CurrSesnDatabag.Fntlist = FntsList;
SeSn.Modify(Act.Add, App.VarNms._CurrentSesionGlobals, CurrSesnDatabag);
}
else
FntsList = ExtractSesnVar().Fntlist;
return AutoComplete.FromListStr(prefixText, FntsList);
}
so a non-static ExtractSesnVar() is required for work within
a static web method FontnmsList_AutCpltDataSrc() scope.
so it makes me wonder …
What basic knowledge am I lacking here (:
I mean, did you ever get in to this junction when you started learning .net ?
and as for what is called in SO A Real Question :
what is the work around as a solution to this scenario,(its only one example . as there should be many other ‘junctions’ like this one, that you could come up with)
I should think there’s supposed to be some way, to bridge between these two entities while writing I could think of passing in data which is non static as parameter ,
so what am i missing here . what is the correct solution as for the subjetced code above ?
I’m assuming you’re asking: How can I call instance methods from a static method?
There are only 3 options:
Sometimes, instance methods only require 1 or perhaps 2 pieces of state information from the instance and in those cases it’s easier to turn them into static and have the instance method call the static method with the extra bit of state information. Check if this is the case here.
for example, imagine you have a very simple class like this:
and you had a static method that needed to call Add, you could refactor the above code into below code so that you didn’t have to create an instance anymore in your static method, but still had the Add logic in one place:
I’ve chosen to make the static Add method internal so only other classes in your assembly and friend assemblies can call it. Feel free to make it public if it makes sense in your scenario.
None of this is ideal, but it might be worthwhile in your case. YMMV.