class session variables

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default 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("myDBComp") = 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 initializecomponent()

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

    Where is it supposed to go?
    Thanks,
    Dean


    Dean Guest

  2. Similar Questions and Discussions

    1. Instance- and class-variables (was mixing in class methods)
      Hi -- On Thu, 2 Oct 2003, Mark J. Reed wrote: Also, every object should have the right to maintain state in instance variables that are...
    2. Instance- and class-variables (was mixing in class methods)
      Hi -- On Fri, 3 Oct 2003 dblack@superlink.net wrote: And of course the fact that it might actually be self.class.class_eval { @var } also...
    3. Instance- and class-variables (was mixing in class methods)
      Hi -- On Thu, 2 Oct 2003, Gavin Sinclair wrote: Ummm, what array? :-) I think you'd need something like: class Project class << self...
    4. Session Variables in a Class
      Hi everyone, I've been experimenting with sessions. I can make them work just fine in a normal PHP page. But when I create a class and try to...
    5. class - session variables
      Probably not a good idea. I thing the general trend is to minimize the amount of session variables because they take up memory on your web server...
  3. #2

    Default 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@earthlink.net> wrote in message
    news:eais2vsWDHA.1736@TK2MSFTNGP10.phx.gbl...
    > 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("myDBComp") = 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 initializecomponent()
    >
    > 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 Guest

  4. #3

    Default 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 initializecomponent() 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@hotmail.com> wrote in message
    news:uiJsZWtWDHA.3232@tk2msftngp13.phx.gbl...
    > 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@earthlink.net> wrote in message
    > news:eais2vsWDHA.1736@TK2MSFTNGP10.phx.gbl...
    > > 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("myDBComp") = 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 initializecomponent()
    > >
    > > But then it disappears if I make any changed to the UI in the aspx page.
    > >
    > > Where is it supposed to go?
    > > Thanks,
    > > Dean
    > >
    > >
    >
    >

    Dean Guest

  5. #4

    Default 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 - initializecomponent is meant for designer generated code.
    page_init is yours to customize, those people usually use page_load.


    "Dean" <deanblakely@earthlink.net> wrote in message
    news:%2350qoBuWDHA.2100@TK2MSFTNGP11.phx.gbl...
    > 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 initializecomponent() 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@hotmail.com> wrote in message
    > news:uiJsZWtWDHA.3232@tk2msftngp13.phx.gbl...
    > > 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@earthlink.net> wrote in message
    > > news:eais2vsWDHA.1736@TK2MSFTNGP10.phx.gbl...
    > > > 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("myDBComp") = 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 initializecomponent()
    > > >
    > > > 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 Guest

  6. #5

    Default 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 InitializeComponent() (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 InitializeComponent() and re insert it each time it gets
    wiped out upon UI change.

    "Marina" <mzlatkina@hotmail.com> wrote in message
    news:OkWFgJvWDHA.1872@TK2MSFTNGP12.phx.gbl...
    > 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 - initializecomponent is meant for designer generated code.
    > page_init is yours to customize, those people usually use page_load.
    >
    >
    > "Dean" <deanblakely@earthlink.net> wrote in message
    > news:%2350qoBuWDHA.2100@TK2MSFTNGP11.phx.gbl...
    > > 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 initializecomponent() 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@hotmail.com> wrote in message
    > > news:uiJsZWtWDHA.3232@tk2msftngp13.phx.gbl...
    > > > 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@earthlink.net> wrote in message
    > > > news:eais2vsWDHA.1736@TK2MSFTNGP10.phx.gbl...
    > > > > 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("myDBComp") = 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 initializecomponent()
    > > > >
    > > > > But then it disappears if I make any changed to the UI in the aspx
    > page.
    > > > >
    > > > > Where is it supposed to go?
    > > > > Thanks,
    > > > > Dean
    > > > >
    > > > >
    > > >
    > > >
    > >
    > >
    >
    >

    Tina Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139