By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,749 Members | 1,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,749 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Nov 24 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
int iCount = ((int)Application["TotalPlayings"])++;
Response.Write("count = " + iCount); //makes sense :-)
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"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.

Nov 24 '05 #2

P: n/a
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On 24 Nov 2005 09:25:24 -0800, "farmer" <sd******@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.


Nov 25 '05 #3

P: n/a
Couldn't understand very much.Instead "Application[""]" with "static
int"?Sound very well,but how to use it in other pages?

Nov 25 '05 #4

P: n/a
> 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
Nov 25 '05 #5

P: n/a

//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? :-)

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"farmer" wrote:
Couldn't understand very much.Instead "Application[""]" with "static
int"?Sound very well,but how to use it in other pages?

Nov 25 '05 #6

P: n/a
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();

--
Scott
http://www.OdeToCode.com/blogs/scott/

On 25 Nov 2005 05:43:26 -0800, "farmer" <sd******@sina.com> wrote:
Couldn't understand very much.Instead "Application[""]" with "static
int"?Sound very well,but how to use it in other pages?


Nov 26 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.