<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.iis.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:cs="http://blogs.iis.net/"><channel><title>Static variable, Shared variable... does it matter?</title><link>http://blogs.iis.net/brian-murphy-booth/archive/2007/06/15/static-shared-or-not-who-cares.aspx</link><description>Introduction Occasionally I am challenged with the task of needing to explain what the difference is between a "regular" variable and a "static" variable (called "Shared" in VB.NET). I use the term "challenged" not because the people I explain to aren</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>re: Static variable, Shared variable... does it matter?</title><link>http://blogs.iis.net/brian-murphy-booth/archive/2007/06/15/static-shared-or-not-who-cares.aspx#2937472</link><pubDate>Thu, 12 Feb 2009 18:33:53 GMT</pubDate><guid isPermaLink="false">50bcf3b4-f6fe-4638-adff-0c150e922e99:2937472</guid><dc:creator>satwik</dc:creator><description>&lt;p&gt;I think the static variables are so &amp;quot;playfully&amp;quot; misused. I guess new developers should read this article posted by Brian to understand the trouble they are getting into if they use statics without thinking.&lt;/p&gt;
&lt;p&gt;Excellent post!!&lt;/p&gt;
&lt;img src="http://blogs.iis.net/aggbug.aspx?PostID=2937472" width="1" height="1"&gt;</description></item><item><title>re: Static variable, Shared variable... does it matter?</title><link>http://blogs.iis.net/brian-murphy-booth/archive/2007/06/15/static-shared-or-not-who-cares.aspx#2842913</link><pubDate>Fri, 02 Jan 2009 14:43:47 GMT</pubDate><guid isPermaLink="false">50bcf3b4-f6fe-4638-adff-0c150e922e99:2842913</guid><dc:creator>brian-murphy-booth</dc:creator><description>&lt;p&gt;Sandhya,&lt;/p&gt;
&lt;p&gt;&amp;quot;Shared&amp;quot; (static) refers to whether a variable is specific to a &amp;quot;new&amp;quot; instance of an object/class or whether it is not specific to a specific instance. &amp;quot;Private&amp;quot; refers to a variable's visibility outside the class. It will often make sense to have a shared variable that is private. Quick example: If you have an array of country names that you want to use to populate a DropDownList then making it static might improve performance and scalability. If you had 30 users on your site, it would be wasteful to have 30 copies of your country list in memory. Being Shared would make it one copy. Then, if only one ASPX page and/or class ever used this country list, it might make sense to hide that list from other classes by making it private. It probably wouldn't hurt to make it public, but private would give your class a cleaner look when programming around it.&lt;/p&gt;
&lt;p&gt;But then again, if there is no &amp;quot;Private&amp;quot; or &amp;quot;Public&amp;quot; etc modifier the default would be to be a private variable anyway. :-)&lt;/p&gt;
&lt;img src="http://blogs.iis.net/aggbug.aspx?PostID=2842913" width="1" height="1"&gt;</description></item><item><title>re: Static variable, Shared variable... does it matter?</title><link>http://blogs.iis.net/brian-murphy-booth/archive/2007/06/15/static-shared-or-not-who-cares.aspx#2795490</link><pubDate>Tue, 09 Dec 2008 12:41:19 GMT</pubDate><guid isPermaLink="false">50bcf3b4-f6fe-4638-adff-0c150e922e99:2795490</guid><dc:creator>Satyanarayan Yadav</dc:creator><description>&lt;p&gt;Your article is so good about static variable and anybody can understand very easily the basic fundamental of static variable.Would you please tell me in which situation we should use static variable.&lt;/p&gt;
&lt;img src="http://blogs.iis.net/aggbug.aspx?PostID=2795490" width="1" height="1"&gt;</description></item><item><title>re: Static variable, Shared variable... does it matter?</title><link>http://blogs.iis.net/brian-murphy-booth/archive/2007/06/15/static-shared-or-not-who-cares.aspx#2785204</link><pubDate>Thu, 04 Dec 2008 13:26:50 GMT</pubDate><guid isPermaLink="false">50bcf3b4-f6fe-4638-adff-0c150e922e99:2785204</guid><dc:creator>Sndhyabhavani</dc:creator><description>&lt;p&gt;hello brian,&lt;/p&gt;
&lt;p&gt;Excellent Article.&lt;/p&gt;
&lt;p&gt;Is there any difference between Shared and private Shared?&lt;/p&gt;
&lt;p&gt;Same Problem we faced...in my application..?How to solve the problem..&lt;/p&gt;
&lt;p&gt;We declared one variable as shared, to maintain variables between pageloads of the aspx&lt;/p&gt;
&lt;p&gt;can we use private shared ?&lt;/p&gt;
&lt;p&gt;Hope Will give reply&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;
&lt;p&gt;Sandhya&lt;/p&gt;
&lt;img src="http://blogs.iis.net/aggbug.aspx?PostID=2785204" width="1" height="1"&gt;</description></item><item><title>re: Static variable, Shared variable... does it matter?</title><link>http://blogs.iis.net/brian-murphy-booth/archive/2007/06/15/static-shared-or-not-who-cares.aspx#2669196</link><pubDate>Wed, 08 Oct 2008 00:09:09 GMT</pubDate><guid isPermaLink="false">50bcf3b4-f6fe-4638-adff-0c150e922e99:2669196</guid><dc:creator>Steve</dc:creator><description>&lt;p&gt;Just came across - good examples. Just a quick note on the response above, the author never said to use static sqlConnection objects - he gave an example recommendation of using a static sql connection string which is a good idea. Big difference between a connection object and the connection string initializer. Using a static connection string is a pretty gauranteed way to use connection pooling as your connection objects should be identical if instantiated from the same connection string.&lt;/p&gt;
&lt;img src="http://blogs.iis.net/aggbug.aspx?PostID=2669196" width="1" height="1"&gt;</description></item><item><title>re: Static variable, Shared variable... does it matter?</title><link>http://blogs.iis.net/brian-murphy-booth/archive/2007/06/15/static-shared-or-not-who-cares.aspx#2662590</link><pubDate>Sat, 04 Oct 2008 07:03:03 GMT</pubDate><guid isPermaLink="false">50bcf3b4-f6fe-4638-adff-0c150e922e99:2662590</guid><dc:creator>sharmilla</dc:creator><description>&lt;p&gt;hi very good post.&lt;/p&gt;
&lt;p&gt;Acutally we got the same issue because of static Variable used in the Aspx.cs for assigning ordernumber.&lt;/p&gt;
&lt;p&gt;concurrent user accessing the same page this issue raised in the server. &lt;/p&gt;
&lt;p&gt;now we have removed all the static variables in the page level. but Business layer Still so many methods having static.could you suggest me where static method have to be used?&lt;/p&gt;
&lt;img src="http://blogs.iis.net/aggbug.aspx?PostID=2662590" width="1" height="1"&gt;</description></item><item><title>re: Static variable, Shared variable... does it matter?</title><link>http://blogs.iis.net/brian-murphy-booth/archive/2007/06/15/static-shared-or-not-who-cares.aspx#2382962</link><pubDate>Tue, 27 May 2008 13:04:30 GMT</pubDate><guid isPermaLink="false">50bcf3b4-f6fe-4638-adff-0c150e922e99:2382962</guid><dc:creator>brian-murphy-booth</dc:creator><description>&lt;p&gt;A static variable is not &amp;quot;page&amp;quot; specific. It is &amp;quot;AppDomain&amp;quot; specific. The only relation to the &amp;quot;page&amp;quot; would be the path (so to speak) of the variable (MyProject.MyPage.MyVariable for example). Because all users of your application are running in the same AppDomain (i.e. same IIS application folder), then they will all use the same static variable. So... eventually your users are going to see each other's information since they are all sharing that one single static variable.&lt;/p&gt;
&lt;img src="http://blogs.iis.net/aggbug.aspx?PostID=2382962" width="1" height="1"&gt;</description></item><item><title>re: Static variable, Shared variable... does it matter?</title><link>http://blogs.iis.net/brian-murphy-booth/archive/2007/06/15/static-shared-or-not-who-cares.aspx#2360013</link><pubDate>Thu, 15 May 2008 13:40:30 GMT</pubDate><guid isPermaLink="false">50bcf3b4-f6fe-4638-adff-0c150e922e99:2360013</guid><dc:creator>Pranay</dc:creator><description>&lt;p&gt;Thanks Brian! We recently wrote a big web application for one of our clients, and after everything was done, during a review.. I was shocked..!! almost all the variables had a keyword &amp;quot;static&amp;quot;.. across all the pages!! This explaination is too good to convince my team that we are not going the correct way!!&lt;/p&gt;
&lt;img src="http://blogs.iis.net/aggbug.aspx?PostID=2360013" width="1" height="1"&gt;</description></item><item><title>re: Static variable, Shared variable... does it matter?</title><link>http://blogs.iis.net/brian-murphy-booth/archive/2007/06/15/static-shared-or-not-who-cares.aspx#2327702</link><pubDate>Tue, 29 Apr 2008 14:08:55 GMT</pubDate><guid isPermaLink="false">50bcf3b4-f6fe-4638-adff-0c150e922e99:2327702</guid><dc:creator>Ron Henderson</dc:creator><description>&lt;p&gt;I am using static variables in my aspx.cs to maintain variables between pageloads of the aspx. Won&amp;#39;t each browser get a separate instance of the aspx/aspx.cs so that static variables won&amp;#39;t be shared between sessions?&lt;/p&gt;
&lt;img src="http://blogs.iis.net/aggbug.aspx?PostID=2327702" width="1" height="1"&gt;</description></item><item><title>re: Static variable, Shared variable... does it matter?</title><link>http://blogs.iis.net/brian-murphy-booth/archive/2007/06/15/static-shared-or-not-who-cares.aspx#1905491</link><pubDate>Wed, 12 Sep 2007 17:28:56 GMT</pubDate><guid isPermaLink="false">50bcf3b4-f6fe-4638-adff-0c150e922e99:1905491</guid><dc:creator>brian-murphy-booth</dc:creator><description>&lt;p&gt;Actually, you should not use a static SqlConnection either if you want users to receive different data. The problem is that the SqlConnection is written for single threaded efficiency. If 2 unique SqlCommands are sharing the same SqlConnection then only one of them will win in regards to what the contents of the insternal data stream (TdsParser object) is that comes from SQL. If you *want* all users to share the same data then you can make both SqlConnection and SqlCommand static. But if your goal is better performance it is best to stick with the build in connection pooling of ADO.NET. If you are currently using a static SqlConnection and have not seen problems then I think you have just been lucky so far.&lt;/p&gt;
&lt;img src="http://blogs.iis.net/aggbug.aspx?PostID=1905491" width="1" height="1"&gt;</description></item><item><title>re: Static variable, Shared variable... does it matter?</title><link>http://blogs.iis.net/brian-murphy-booth/archive/2007/06/15/static-shared-or-not-who-cares.aspx#1904590</link><pubDate>Wed, 12 Sep 2007 11:06:53 GMT</pubDate><guid isPermaLink="false">50bcf3b4-f6fe-4638-adff-0c150e922e99:1904590</guid><dc:creator>sanjay</dc:creator><description>gud post brian 

but here the problem is due to sqlCommand being static not sqlconnection.

sqlconnection can very well be static.In fact we can even restrict the sqlconnection instance to one for better performance

woz say?&lt;img src="http://blogs.iis.net/aggbug.aspx?PostID=1904590" width="1" height="1"&gt;</description></item></channel></rss>