class session variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dean

    #1

    class session variables

    I finally got class session variables to work by putting the following in
    global.asax in the session_start:
    dim myDBComp as DBComp = new DBComp........
    session("myDBCo mp") = myDBComp

    In each aspx codebehind module I can define my object right after the class
    definition as so:
    dim myDBComp as DBComp

    The problem I had was where to put the instantiation:
    myDBComp = CType(session(" myDBComp"), DBComp)

    I can't put it up with the definition as the compiler wants only
    declarations up there.
    I can't put it in any of the methods because it loses scope outside the
    methods.
    I finally found that it works if I put in in initializecompo nent()

    But then it disappears if I make any changed to the UI in the aspx page.

    Where is it supposed to go?
    Thanks,
    Dean


  • Marina

    #2
    Re: class session variables

    The instantiation will last the object while the page object exists. Putting
    it in page_init or page_load, will mean this variable is set until the page
    finishes executing.

    "Dean" <deanblakely@ea rthlink.net> wrote in message
    news:eais2vsWDH A.1736@TK2MSFTN GP10.phx.gbl...[color=blue]
    > I finally got class session variables to work by putting the following in
    > global.asax in the session_start:
    > dim myDBComp as DBComp = new DBComp........
    > session("myDBCo mp") = myDBComp
    >
    > In each aspx codebehind module I can define my object right after the[/color]
    class[color=blue]
    > definition as so:
    > dim myDBComp as DBComp
    >
    > The problem I had was where to put the instantiation:
    > myDBComp = CType(session(" myDBComp"), DBComp)
    >
    > I can't put it up with the definition as the compiler wants only
    > declarations up there.
    > I can't put it in any of the methods because it loses scope outside the
    > methods.
    > I finally found that it works if I put in in initializecompo nent()
    >
    > But then it disappears if I make any changed to the UI in the aspx page.
    >
    > Where is it supposed to go?
    > Thanks,
    > Dean
    >
    >[/color]


    Comment

    • Dean

      #3
      Re: class session variables

      Marina:
      According to a test that I just ran, it went out of scope as soon as the
      page_load event ended. Page_init is part of the "web form designer
      generated code", in fact initializecompo nent() is called from there so
      manually entered code gets clobbered there just like code in initialize
      component.

      So, unless I have made a mistake in my test, question still stands.
      thanks,
      Dean

      "Marina" <mzlatkina@hotm ail.com> wrote in message
      news:uiJsZWtWDH A.3232@tk2msftn gp13.phx.gbl...[color=blue]
      > The instantiation will last the object while the page object exists.[/color]
      Putting[color=blue]
      > it in page_init or page_load, will mean this variable is set until the[/color]
      page[color=blue]
      > finishes executing.
      >
      > "Dean" <deanblakely@ea rthlink.net> wrote in message
      > news:eais2vsWDH A.1736@TK2MSFTN GP10.phx.gbl...[color=green]
      > > I finally got class session variables to work by putting the following[/color][/color]
      in[color=blue][color=green]
      > > global.asax in the session_start:
      > > dim myDBComp as DBComp = new DBComp........
      > > session("myDBCo mp") = myDBComp
      > >
      > > In each aspx codebehind module I can define my object right after the[/color]
      > class[color=green]
      > > definition as so:
      > > dim myDBComp as DBComp
      > >
      > > The problem I had was where to put the instantiation:
      > > myDBComp = CType(session(" myDBComp"), DBComp)
      > >
      > > I can't put it up with the definition as the compiler wants only
      > > declarations up there.
      > > I can't put it in any of the methods because it loses scope outside the
      > > methods.
      > > I finally found that it works if I put in in initializecompo nent()
      > >
      > > But then it disappears if I make any changed to the UI in the aspx page.
      > >
      > > Where is it supposed to go?
      > > Thanks,
      > > Dean
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Marina

        #4
        Re: class session variables

        If the variable is declared at the class level, and initialized in page_init
        or page_load, it will stick around for the lifetime of the page - which is
        just until it finishes executing and when the response is sent to the
        browser.

        If you are declaring the variable inside a method - then it will have only
        the scope of the method. This is why it has to be a class level variable.
        After that, you can initialize it where ever you want - and it will stay
        around for the lifetime of the object.

        I don't know what test you did, but this is how it works. You can put code
        into page_init - initializecompo nent is meant for designer generated code.
        page_init is yours to customize, those people usually use page_load.


        "Dean" <deanblakely@ea rthlink.net> wrote in message
        news:%2350qoBuW DHA.2100@TK2MSF TNGP11.phx.gbl. ..[color=blue]
        > Marina:
        > According to a test that I just ran, it went out of scope as soon as the
        > page_load event ended. Page_init is part of the "web form designer
        > generated code", in fact initializecompo nent() is called from there so
        > manually entered code gets clobbered there just like code in initialize
        > component.
        >
        > So, unless I have made a mistake in my test, question still stands.
        > thanks,
        > Dean
        >
        > "Marina" <mzlatkina@hotm ail.com> wrote in message
        > news:uiJsZWtWDH A.3232@tk2msftn gp13.phx.gbl...[color=green]
        > > The instantiation will last the object while the page object exists.[/color]
        > Putting[color=green]
        > > it in page_init or page_load, will mean this variable is set until the[/color]
        > page[color=green]
        > > finishes executing.
        > >
        > > "Dean" <deanblakely@ea rthlink.net> wrote in message
        > > news:eais2vsWDH A.1736@TK2MSFTN GP10.phx.gbl...[color=darkred]
        > > > I finally got class session variables to work by putting the following[/color][/color]
        > in[color=green][color=darkred]
        > > > global.asax in the session_start:
        > > > dim myDBComp as DBComp = new DBComp........
        > > > session("myDBCo mp") = myDBComp
        > > >
        > > > In each aspx codebehind module I can define my object right after the[/color]
        > > class[color=darkred]
        > > > definition as so:
        > > > dim myDBComp as DBComp
        > > >
        > > > The problem I had was where to put the instantiation:
        > > > myDBComp = CType(session(" myDBComp"), DBComp)
        > > >
        > > > I can't put it up with the definition as the compiler wants only
        > > > declarations up there.
        > > > I can't put it in any of the methods because it loses scope outside[/color][/color][/color]
        the[color=blue][color=green][color=darkred]
        > > > methods.
        > > > I finally found that it works if I put in in initializecompo nent()
        > > >
        > > > But then it disappears if I make any changed to the UI in the aspx[/color][/color][/color]
        page.[color=blue][color=green][color=darkred]
        > > >
        > > > Where is it supposed to go?
        > > > Thanks,
        > > > Dean
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Tina

          #5
          Re: class session variables

          There is deffinitely a difference in the life of a component depending if it
          is initialized in Page_init or Page load:
          I first declare my component at the class level of my page as follows:
          Dim myDBProc As PhotoDBProcs

          I then initialize it in the page_load as follows:

          myDBProc = CType(Session(" myDBProc"), PhotoDBProcs)

          I use the component to build content and, at the end of the page_load it
          response is sent to the browser.

          One of the buttons gets pushed triggering an event on the page. In the
          button event the component no longer exists.

          When I initialize this component in InitializeCompo nent() (or page_init),
          however, the component still exists when the button is pushed.

          Your first paragraph on your response is technicaly true but that is not
          sufficient for any real application that has many interactions on a page.

          So, to get the necessary scope of life on this component, I need to
          initialize it in InitializeCompo nent() and re insert it each time it gets
          wiped out upon UI change.

          "Marina" <mzlatkina@hotm ail.com> wrote in message
          news:OkWFgJvWDH A.1872@TK2MSFTN GP12.phx.gbl...[color=blue]
          > If the variable is declared at the class level, and initialized in[/color]
          page_init[color=blue]
          > or page_load, it will stick around for the lifetime of the page - which is
          > just until it finishes executing and when the response is sent to the
          > browser.
          >
          > If you are declaring the variable inside a method - then it will have only
          > the scope of the method. This is why it has to be a class level variable.
          > After that, you can initialize it where ever you want - and it will stay
          > around for the lifetime of the object.
          >
          > I don't know what test you did, but this is how it works. You can put[/color]
          code[color=blue]
          > into page_init - initializecompo nent is meant for designer generated code.
          > page_init is yours to customize, those people usually use page_load.
          >
          >
          > "Dean" <deanblakely@ea rthlink.net> wrote in message
          > news:%2350qoBuW DHA.2100@TK2MSF TNGP11.phx.gbl. ..[color=green]
          > > Marina:
          > > According to a test that I just ran, it went out of scope as soon as the
          > > page_load event ended. Page_init is part of the "web form designer
          > > generated code", in fact initializecompo nent() is called from there so
          > > manually entered code gets clobbered there just like code in initialize
          > > component.
          > >
          > > So, unless I have made a mistake in my test, question still stands.
          > > thanks,
          > > Dean
          > >
          > > "Marina" <mzlatkina@hotm ail.com> wrote in message
          > > news:uiJsZWtWDH A.3232@tk2msftn gp13.phx.gbl...[color=darkred]
          > > > The instantiation will last the object while the page object exists.[/color]
          > > Putting[color=darkred]
          > > > it in page_init or page_load, will mean this variable is set until the[/color]
          > > page[color=darkred]
          > > > finishes executing.
          > > >
          > > > "Dean" <deanblakely@ea rthlink.net> wrote in message
          > > > news:eais2vsWDH A.1736@TK2MSFTN GP10.phx.gbl...
          > > > > I finally got class session variables to work by putting the[/color][/color][/color]
          following[color=blue][color=green]
          > > in[color=darkred]
          > > > > global.asax in the session_start:
          > > > > dim myDBComp as DBComp = new DBComp........
          > > > > session("myDBCo mp") = myDBComp
          > > > >
          > > > > In each aspx codebehind module I can define my object right after[/color][/color][/color]
          the[color=blue][color=green][color=darkred]
          > > > class
          > > > > definition as so:
          > > > > dim myDBComp as DBComp
          > > > >
          > > > > The problem I had was where to put the instantiation:
          > > > > myDBComp = CType(session(" myDBComp"), DBComp)
          > > > >
          > > > > I can't put it up with the definition as the compiler wants only
          > > > > declarations up there.
          > > > > I can't put it in any of the methods because it loses scope outside[/color][/color]
          > the[color=green][color=darkred]
          > > > > methods.
          > > > > I finally found that it works if I put in in initializecompo nent()
          > > > >
          > > > > But then it disappears if I make any changed to the UI in the aspx[/color][/color]
          > page.[color=green][color=darkred]
          > > > >
          > > > > Where is it supposed to go?
          > > > > Thanks,
          > > > > Dean
          > > > >
          > > > >
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          Working...