Supposed global variable keeps reverting back to 0

Last post 01-07-2007 4:42 AM by Merlot786. 3 replies.

Sort Posts:

  • Supposed global variable keeps reverting back to 0

    01-06-2007, 6:16 PM
    • Member
      point Member
    • Merlot786
    • Member since 12-18-2006, 3:49 PM
    • Posts 7

    I'm a newbie to Asp.Net 2.0 so it's probably me doing something wrong but I'm trying to modify this excellent Asp.Net chat room (http://steveorr.net/articles/WebChat.aspx). I've delved into the coding and I've tried to insert my own variable into the coding that can hold data inbetween server calls. I've named it MyVariable in the extract below...

    Public

    Class WebChat Inherits WebControl Implements System.Web.UI.ICallbackEventHandler, INamingContainer

      Private _Messages As System.Collections.Generic.Queue(Of ChatMessage)

      Private _LastMsgID As Long = 0

      Private MyVariable As Long

    Within the class theres a RaiseCallBackEvent function gets called when someone is chatting, into which I've inserted:

    MyVariable = MyVariable +1

    Finally there's the GetCallbackResult function in which I print back MyVariable. However the value is always 1, where as in  theory it ought to increase each time the function is called surely?

  • Re: Supposed global variable keeps reverting back to 0

    01-06-2007, 8:56 PM
    Answer
    • Contributor
      3,803 point Contributor
    • rojay12
    • Member since 04-03-2006, 10:43 PM
    • Sacramento, CA
    • Posts 695

    try using a viewstate variable or a application variable and see if you have the same results. I alway do something like this...

      Private Property MyVariable() As Long
        Get
          Return ViewState("MyVariable")
        End Get
        Set(ByVal value As Long)
          ViewState("MyVariable") = value
        End Set
      End Property

     

    Jared Roberts
    Lead Application Developer
  • Re: Supposed global variable keeps reverting back to 0

    01-07-2007, 12:28 AM
    Answer
    • Member
      670 point Member
    • scommisso
    • Member since 07-02-2004, 12:11 AM
    • Phoenix, AZ, USA
    • Posts 139

    ^^^^ Exactly -- that will solve it completely. The reason why your code isn't working is that the page class, including all of the controls, are atomic to a single request. In order for values to persist, you need to use some sort of persistance mechanism. ViewState is what the .NET server controls use to maintain state.

    When working with viewstate, you need to pay attention to when the 'Save Viewstate' and 'Load Viewstate' events fire. If you see some strange persistance problems pop up, it's probably because values are being set after viewstate is saved, or before viewstate is loaded. Here is a link to a good diagram:

    http://www.eggheadcafe.com/articles/o_aspNet_Page_LifeCycle.jpg

     

    Steve Commisso
    MCSD.NET, MCPD: EAD
  • Re: Supposed global variable keeps reverting back to 0

    01-07-2007, 4:42 AM
    • Member
      point Member
    • Merlot786
    • Member since 12-18-2006, 3:49 PM
    • Posts 7

    Thanks guys - that was very useful. I was initially a bit thrown by the problem because I hadn't spotted the following line in the code:

    Context.Application(WebChatTopic) = _Messages

    Which left me wondering why the _Messages variable kepts its state but mine didn't. All makes sense now!

     

    Cheers

Page 1 of 1 (4 items)