简体   繁体   中英

failed to update a static property

public static class clsCounter
{
    static int count;

    public static int Counter
    {
        get { return count; }
        set { count = value; }
    }
}

The above is the static class that is used to record a number.

Also, I have two projects within a VS2010 solution, one of which is a class library. In one of these classes, I have got the following code which uses clsCounter .

if (clsCounter.Counter == 0)
    countIES++;
else
    countIES = 0;

Now, in the other project, I set some new values to clsCounter

clsCounter.Counter = 50;

However, for some reason, I am not able to set clsCounter.Counter to 50, thus I always get countIES++ . The code looks okay to me, and I have no idea what's wrong with it? Can anyone help?

Thanks.

EDIT:

I wonder if it has something to do with the scope of projects within vs solution?

Solution Structure

Solution

  • ExcelAddIn

    • Form1.cs => (clsCounter.Counter = 50)
    • ...
  • ClassLibrary

    • clsCounter => (static class)
    • ...

EDIT 2:

clsCounter.Counter = 50; is actually running in backgroundworker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) event. Could this be a possible issue?

EDIT 3:

I have uploaded a sample project that seems to be able to reproduce the same problem. Here's the shared link: => http://www.4shared.com/folder/sInyNWyi/_online.html

What I would like to do here is to populate a cell with value, Other case after the button 'set value' is pressed. The static class and UDF can be found in the class library.

Note that, to be able to use =testFunc() within excel addin, need to find it in automation server list and enable it. So just go File->Option->Addin->Under Manage Add-in->Click GO->Automation->Ebale ClassLibrary1.UDF

Please also check if the option " Register for COM interop " has been enabled or not before launching the debugger. To find it, go ClassLibrary1 Property -> Build -> Under Output, check Register for COM interop .

Add the following line to your static property:

public static class clsCounter
{
    private static int count;

    public static int Counter
    {
        get { 
               Debug.WriteLine("Counter viewed"); 
               return count; 
            }
        set { 
               Debug.WriteLine("Counter Changed from {0} to {1}", count, value);
               count = value; 
            }
    }
}

Then you can watch your debugger and set breakpoints on the counter which will allow you to find out which part of the code is modifying your counter inappropriately. A static counter will be initialised "at some time" before it is accessed. I would say you are setting the counter and something somewhere is immediately incrementing it before you read it.

Finally, I think I found a workaround although it had nothing to do with the static. I was kinda inspired by the idea of using cookies in the web apps.

Similarly, all I need to do here is:

store a value in a temporary text file, by doing

System.IO.File.WriteAllText(@"C:\countIESValue.txt", value);

in the "set value" button click event handler.

And read the stored value whenever I need it from the above text file and assign it to a local variable.

if(System.IO.File.Exists(@"C:\countIESValue.txt"))
{
    string val = System.IO.File.ReadAllText(@"C:\countIESValue.txt");
}

The text file can also be deleted after done processing. In this way, I don't have to worry about any scope or application domain issues, although the permission of writing files is required. I am glad it worked pretty all right for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM