Geeks With Blogs

News My Blog has been MOVED to https://mfreidge.wordpress.com
Michael Freidgeim's OLD Blog My Blog has been MOVED to https://mfreidge.wordpress.com
In one of static class in our application, I found local static members that were used to pass data between calls of static methods.
It’s wrong and can cause errors that are intermittent and very hard to reproduce.
The problem will happen if the same code executed for 2 users simultaneously. In this case value for one user could be used for the second user and result will be unpredictable.
 
The code was similar the following:
 
    public static class HelperClass
    {
       private static string _dataToPass = "";
   static void Method1(string param)
{
_dataToPass =param;
}
   static void Method2()
      {
//logic based on _dataToPass value;
}
 }
The pattern is popular for instance object, when one method saves state in the instance, and other method use it, but it is not acceptable for static class.
Consider to use singleton pattern, if you have shared for the domain object.
The issue is well known(e.g. see Statics & Thread Safety: Part I   and Part 2 ), but I decided to write about it again.
Posted on Sunday, November 30, 2008 10:49 PM DotNetNuke | Back to top


Comments on this post: Do not pass data between static methods using static data members.

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net