How to assign value to a static variable in a non-static function?

前端 未结 2 2039
予麋鹿
予麋鹿 2021-01-29 09:40

Basically, ajax in aspx is polling the value return from .cs every 1000 ms from a WebMethod in .cs which is static GetData(). A property i

相关标签:
2条回答
  • 2021-01-29 09:45

    Prefix your call to the static method with the class name.

    Downloader.PERCENT = library.returnValue();
    

    However as John points out, unless you can guarantee that you would only have one concurrent user for your application, then you shouldn't be using a static member for this task.

    A better approach might be to store your PERCENT data in a session variable. That way it's only scoped per session, and not for the entire application.

    Session["Percent"] = library.returnValue();
    

    Then change your webmethod like this:

    [WebMethod]
    public static string GetData()
    {
         return (string)Session["Percent"];
    }
    
    0 讨论(0)
  • 2021-01-29 09:59

    To reference a static member, you must prefix the member with the name of the class that defines it.

    However, don't do that!

    Your static variable will be common to every user of your web service. You may think it would be one copy per session, but that's not the case. There will be one copy for all users of the same web service.

    0 讨论(0)
提交回复
热议问题