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

Discussion in 'ASP .Net' started by farmer, Nov 24, 2005.

  1. farmer

    farmer Guest

    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.
     
    farmer, Nov 24, 2005
    #1
    1. Advertising

  2. 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.
    >
    >
     
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Nov 24, 2005
    #2
    1. Advertising

  3. farmer

    Scott Allen Guest

    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" <> 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.
     
    Scott Allen, Nov 25, 2005
    #3
  4. farmer

    farmer Guest

    Couldn't understand very much.Instead "Application[""]" with "static
    int"?Sound very well,but how to use it in other pages?
     
    farmer, Nov 25, 2005
    #4
  5. farmer

    Hans Kesting Guest

    Re: =?UTF-8?B?QXBwbGljYXRpb25bIlRvdGFsUGxheWluZ3MiXSsrIGRvZXNuJ3Qg?==?UTF-8?B?d29yaw==?=

    > 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
     
    Hans Kesting, Nov 25, 2005
    #5
  6. //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?
    >
    >
     
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Nov 25, 2005
    #6
  7. farmer

    Scott Allen Guest

    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" <> wrote:

    >Couldn't understand very much.Instead "Application[""]" with "static
    >int"?Sound very well,but how to use it in other pages?
     
    Scott Allen, Nov 26, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sherif ElMetainy

    GC doesn't work in my ASP.NET application

    Sherif ElMetainy, Dec 3, 2004, in forum: ASP .Net
    Replies:
    11
    Views:
    965
    David Browne
    Dec 4, 2004
  2. rehto
    Replies:
    0
    Views:
    939
    rehto
    Jun 1, 2006
  3. MZ
    Replies:
    7
    Views:
    842
    Ed Mullen
    Mar 17, 2008
  4. Tilman
    Replies:
    0
    Views:
    425
    Tilman
    Mar 19, 2008
  5. rigo
    Replies:
    0
    Views:
    182
Loading...

Share This Page