Quantcast
Channel: DotNetNuke – Michael Freidgeim's Blog
Viewing all articles
Browse latest Browse all 10

Do not pass data between static methods using static data members.

$
0
0
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.

Viewing all articles
Browse latest Browse all 10

Trending Articles