Home All Groups Group Topic Archive Search About

Application["TotalPlayings"]++ doesn't work

Author
24 Nov 2005 5:25 PM
farmer
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.

Author
24 Nov 2005 6:05 PM
Phillip Williams
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.
>
>
Are all your drivers up to date? click for free checkup

Author
25 Nov 2005 2:03 AM
Scott Allen
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.
Author
25 Nov 2005 1:43 PM
farmer
Couldn't understand very much.Instead "Application[""]" with  "static
int"?Sound very well,but how to use it in other pages?
Author
25 Nov 2005 6:29 PM
Phillip Williams
//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?
>
>
Author
26 Nov 2005 5:18 AM
Scott Allen
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?
Author
25 Nov 2005 2:58 PM
Hans Kesting
> 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.

Application["TotalPlayings"] returns an object,
(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

Bookmark and Share