如何在非静态函数中为静态变量赋值

如何在非静态函数中为静态变量赋值

本文介绍了如何在非静态函数中为静态变量赋值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,aspx中的ajax每隔1000毫秒从.cs中的WebMethod(static GetData())轮询从.cs返回的值.一个属性被声明为static public static int Percent { get; set; }.我想做的是单击btn1时,它将把值分配到Percent中,而ajaxWebMethod static GetData()中获取值.

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 is declared as static public static int Percent { get; set; } . What i want to do is when a btn1 is clicked, it will assign the value into Percent and the ajax get the value from the WebMethod static GetData().

public static int PERCENT { get; set; }

[WebMethod]
public static string GetData()
{
     return PERCENT ;
}

protected void btn1_Click(object sender, EventArgs e)
{
     DownloadLibrary downloader = new DownloadLibrary();
     downloader.DoWorkSynchronous();
     bool isLibraryBusy = true;
     while (isLibraryBusy)
     {
        PERCENT  = library.returnValue();
        isLibraryBusy = downloader.IsBusy();
     }
}

Downloader.aspx(ajax轮询)

    $(document).ready(function() {
        $("#progressbar").progressbar();
              setTimeout(updateProgress, 100);
    });

   function updateProgress() {
              $.ajax({
                  type: "POST",
                  url: "Downloader.aspx/GetData",
                  data: "{}",
                  contentType: "application/json; charset=utf-8",
                  dataType: "json",
                  async: true,
                  success: function(msg) {
                      // Replace the div's content with the page method's return.

                      $("#progressbar").progressbar("option", "value", msg.d);
                  }
              });
          }

我想将值分配给Percent,但是不幸的是,由于Percentstatic,我无法执行此操作.如果我未将Percent声明为static,则GetData()函数无法验证Percent是什么.对于ajax轮询,GetData()必须位于static中.如何在non-static function中的static变量中分配值?

I want to assign the value to Percent but unfortunately,i am unable to do it because the Percent is static. If i don't declare the Percent as static, the GetData() function unable to verify what Percent is. For ajax polling, GetData() have to be in static. How to assign value to a static variable in a non-static function?

推荐答案

要引用static成员,必须在成员之前加上定义它的类的名称.

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

但是,不要那样做!

您的静态变量将对您的Web服务的每个用户都是公用的.您可能会认为每个会话一个副本,但事实并非如此.同一网络服务的所有用户都将获得一份副本.

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.

这篇关于如何在非静态函数中为静态变量赋值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 14:37