|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Application["TotalPlayings"]++ doesn't workCompiler Error Message: CS0201: Only assignment, call, increment,
decrement, and new object expressions can be used as a statement And: (int)Application["TotalPlayings"]++; Makes no sense. int iCount = ((int)Application["TotalPlayings"])++;
Response.Write("count = " + iCount); //makes sense :-) Show quoteHide quote "farmer" wrote: > Compiler Error Message: CS0201: Only assignment, call, increment, > decrement, and new object expressions can be used as a statement > > And: > (int)Application["TotalPlayings"]++; > Makes no sense. > > It would be faster (and safer) to declare a static int field in some
class (Global.asax, perhaps), and use Interlocked.Increment to be thread safe. Show quoteHide quote On 24 Nov 2005 09:25:24 -0800, "farmer" <sdfar***@sina.com> wrote: >Compiler Error Message: CS0201: Only assignment, call, increment, >decrement, and new object expressions can be used as a statement > >And: >(int)Application["TotalPlayings"]++; >Makes no sense. Couldn't understand very much.Instead "Application[""]" with "static
int"?Sound very well,but how to use it in other pages? //http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iissdk/html/7b41355f-5050-4b2b-ac1c-d10dc206b46a.asp
Application.Lock(); if (Application["hitCount"] == null) { Application["hitCount"] = 1; } else { Application["hitCount"]= ((int)Application["hitCount"])+1; } Application.UnLock(); if (Application["hitCount2"] == null) { Application["hitCount2"] = 1; } else { //use Interlocked.Increment Method (thanks to Scott Allen for introducing the idea) //http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemthreadinginterlockedclasstopic.asp int iCount2 = (int)Application["hitCount2"]; Application["hitCount2"]=Interlocked.Increment(ref iCount2); } Response.Write ("Application count= " + Application["hitCount"]); Response.Write ("Application count2= " + Application["hitCount2"]); //How many programmers does it take to increment an application variable? :-) Show quoteHide quote "farmer" wrote: > Couldn't understand very much.Instead "Application[""]" with "static > int"?Sound very well,but how to use it in other pages? > > It would look like:
using System.Threading; public class HitCounter { static int _counter; public static int IncrementCounter() { return Interlocked.Increment(ref _counter); } public static int GetCounter() { return _counter; } } You could use it like: int x = HitCounter.IncrementCounter(); Show quoteHide quote On 25 Nov 2005 05:43:26 -0800, "farmer" <sdfar***@sina.com> wrote: >Couldn't understand very much.Instead "Application[""]" with "static >int"?Sound very well,but how to use it in other pages? > Compiler Error Message: CS0201: Only assignment, call, increment, Application["TotalPlayings"] returns an object,> decrement, and new object expressions can be used as a statement > > And: > (int)Application["TotalPlayings"]++; > Makes no sense. (int)Application["TotalPlayings"] unboxes that object and returns an int as a *copy* of that value, When you use the "++" operator, you will use that on that copy, not on the original. So the original Application["TotalPlayings"] is not updated. Plus you might have a precedence problem: what would be evaluated first: the (int) or the ++? Hans Kesting
Other interesting topics
Correct syntax for an update stored procedure
About web.config file Setting Profile properties for not logged in users Dynamic content problem Some questions about masterpages. how load one frame before another in aspx page The server tag is not well formed ASP.NET 2.0 file upload with progress dialog VS 2005: Virtual directories HTML Tables |
|||||||||||||||||||||||