Global variable lifetime

Discussion in 'ASP .Net' started by =?Utf-8?B?VHJhcHVsbw==?=, Jan 26, 2005.

  1. I've a question about globa variables lifetime in an asp.net app.

    I've declared this class:

    Public Class Utils

    Private Shared _FcdDataManagement As FCD.DataManagement

    Public Shared ReadOnly Property FcdDataManagement() As FCD.DataManagement
    Get
    If _FcdDataManagement Is Nothing Then
    _FcdDataManagement = New FCD.DataManagement
    End If

    Return _FcdDataManagement
    End Get
    End Property

    End Class

    I think the variable may be nothing at the first call of each page
    execution. But I've seen that the variable is nothing only for the first call
    of the application. Then if I reload the page (=new execution) the variable
    is not nothing. Why? I think each page execution pipeline is isolated from
    others (otherwise why Application state?), but this behavior is quite
    strange...

    thanks
     
    =?Utf-8?B?VHJhcHVsbw==?=, Jan 26, 2005
    #1
    1. Advertising

  2. =?Utf-8?B?VHJhcHVsbw==?=

    Ollie Riches Guest

    it is because you have marked the variable '_FcdDataManagement' as Shared,
    it is 'shared' across instances of the class there is only one instance of
    the variable and therefore when it is allocated first time it remains
    allocated unless you explicitly set the value to null.

    HTH

    Ollie Riches
    "Trapulo" <> wrote in message
    news:...
    > I've a question about globa variables lifetime in an asp.net app.
    >
    > I've declared this class:
    >
    > Public Class Utils
    >
    > Private Shared _FcdDataManagement As FCD.DataManagement
    >
    > Public Shared ReadOnly Property FcdDataManagement() As

    FCD.DataManagement
    > Get
    > If _FcdDataManagement Is Nothing Then
    > _FcdDataManagement = New FCD.DataManagement
    > End If
    >
    > Return _FcdDataManagement
    > End Get
    > End Property
    >
    > End Class
    >
    > I think the variable may be nothing at the first call of each page
    > execution. But I've seen that the variable is nothing only for the first

    call
    > of the application. Then if I reload the page (=new execution) the

    variable
    > is not nothing. Why? I think each page execution pipeline is isolated

    from
    > others (otherwise why Application state?), but this behavior is quite
    > strange...
    >
    > thanks
    >
     
    Ollie Riches, Jan 26, 2005
    #2
    1. Advertising

  3. "Ollie Riches" wrote:

    > it is because you have marked the variable '_FcdDataManagement' as Shared,
    > it is 'shared' across instances of the class there is only one instance of
    > the variable and therefore when it is allocated first time it remains
    > allocated unless you explicitly set the value to null.
    >
    > HTH


    So I can have also multithreading problems?
    However if this is the problem and the variable is retained in memory until
    asp.net application is unloaded, what is the difference from using a
    page.Application-basd solutions, and using a shared variable?

    To obtain what I want (a property that returns a variable instanced, but
    with lifetime of a single page's execution) the right way is to declare it on
    a module, and than use the property to chech that it is not nothing?

    thanks
     
    =?Utf-8?B?VHJhcHVsbw==?=, Jan 26, 2005
    #3
  4. This is how it is. Shared (static) variables persist between postbacks.

    Eliyahu

    "Trapulo" <> wrote in message
    news:...
    > I've a question about globa variables lifetime in an asp.net app.
    >
    > I've declared this class:
    >
    > Public Class Utils
    >
    > Private Shared _FcdDataManagement As FCD.DataManagement
    >
    > Public Shared ReadOnly Property FcdDataManagement() As

    FCD.DataManagement
    > Get
    > If _FcdDataManagement Is Nothing Then
    > _FcdDataManagement = New FCD.DataManagement
    > End If
    >
    > Return _FcdDataManagement
    > End Get
    > End Property
    >
    > End Class
    >
    > I think the variable may be nothing at the first call of each page
    > execution. But I've seen that the variable is nothing only for the first

    call
    > of the application. Then if I reload the page (=new execution) the

    variable
    > is not nothing. Why? I think each page execution pipeline is isolated

    from
    > others (otherwise why Application state?), but this behavior is quite
    > strange...
    >
    > thanks
    >
     
    Eliyahu Goldin, Jan 26, 2005
    #4
  5. =?Utf-8?B?VHJhcHVsbw==?=

    Ollie Riches Guest

    if you remove the Shared keyword then it will only 'live' for the life time
    of the request

    e.g.

    Public Class Utils

    Private _FcdDataManagement As FCD.DataManagement

    Public Shared ReadOnly Property FcdDataManagement() As
    FCD.DataManagement
    Get
    If _FcdDataManagement Is Nothing Then
    _FcdDataManagement = New FCD.DataManagement
    End If

    Return _FcdDataManagement
    End Get
    End Property

    End Class

    HTH

    Ollie Riches

    "Trapulo" <> wrote in message
    news:...
    > "Ollie Riches" wrote:
    >
    > > it is because you have marked the variable '_FcdDataManagement' as

    Shared,
    > > it is 'shared' across instances of the class there is only one instance

    of
    > > the variable and therefore when it is allocated first time it remains
    > > allocated unless you explicitly set the value to null.
    > >
    > > HTH

    >
    > So I can have also multithreading problems?
    > However if this is the problem and the variable is retained in memory

    until
    > asp.net application is unloaded, what is the difference from using a
    > page.Application-basd solutions, and using a shared variable?
    >
    > To obtain what I want (a property that returns a variable instanced, but
    > with lifetime of a single page's execution) the right way is to declare it

    on
    > a module, and than use the property to chech that it is not nothing?
    >
    > thanks
    >
    >
     
    Ollie Riches, Jan 26, 2005
    #5
  6. If you want "lifetime of a single page's execution", why do you make it
    Shared?

    Eliyahu

    "Trapulo" <> wrote in message
    news:...
    > "Ollie Riches" wrote:
    >
    > > it is because you have marked the variable '_FcdDataManagement' as

    Shared,
    > > it is 'shared' across instances of the class there is only one instance

    of
    > > the variable and therefore when it is allocated first time it remains
    > > allocated unless you explicitly set the value to null.
    > >
    > > HTH

    >
    > So I can have also multithreading problems?
    > However if this is the problem and the variable is retained in memory

    until
    > asp.net application is unloaded, what is the difference from using a
    > page.Application-basd solutions, and using a shared variable?
    >
    > To obtain what I want (a property that returns a variable instanced, but
    > with lifetime of a single page's execution) the right way is to declare it

    on
    > a module, and than use the property to chech that it is not nothing?
    >
    > thanks
    >
    >
     
    Eliyahu Goldin, Jan 26, 2005
    #6
  7. =?Utf-8?B?VHJhcHVsbw==?=

    Trapulo Guest

    I cannot use a non shared variable in a shared method or property...

    "Ollie Riches" <> wrote in message
    news:...
    > if you remove the Shared keyword then it will only 'live' for the life

    time
    > of the request
    >
    > e.g.
    >
    > Public Class Utils
    >
    > Private _FcdDataManagement As FCD.DataManagement
    >
    > Public Shared ReadOnly Property FcdDataManagement() As
    > FCD.DataManagement
    > Get
    > If _FcdDataManagement Is Nothing Then
    > _FcdDataManagement = New FCD.DataManagement
    > End If
    >
    > Return _FcdDataManagement
    > End Get
    > End Property
    >
    > End Class
    >
    > HTH
    >
     
    Trapulo, Jan 26, 2005
    #7
  8. =?Utf-8?B?VHJhcHVsbw==?=

    Trapulo Guest

    Because this variable is a kind of "dispatcher". A shortcut to access to an
    external class (facade) without create an ew instance every time I need to
    execute a business operaration. And in a single page execution I may use it
    more than a single time from different objects (page, controls, event
    hanlders, etc).
    Basically what I was trying is to avoid to create many times an instance of
    the same object, to have best performance. And to have a little less and
    more direct code ("msyharedvariabel.method" only, and not "dim a as new
    mysharedvariable.... a.method").

    Not a big gol, but the behavior I reported was interesting to investigate.


    "Eliyahu Goldin" <> wrote in message
    news:...
    > If you want "lifetime of a single page's execution", why do you make it
    > Shared?
    >
    > Eliyahu
    >
    >
     
    Trapulo, Jan 26, 2005
    #8
  9. =?Utf-8?B?VHJhcHVsbw==?=

    Trapulo Guest

    Interesting. I didn't know this.

    And what is the lifetime in this context? The variable is disposed only when
    the asp.net application ends?


    "Eliyahu Goldin" <> wrote in message
    news:ugUl2#...
    > This is how it is. Shared (static) variables persist between postbacks.
    >
    > Eliyahu
    >
     
    Trapulo, Jan 26, 2005
    #9
  10. =?Utf-8?B?VHJhcHVsbw==?=

    Ollie Riches Guest

    you need to look at the 'class factory' set of design patterns these will
    give you a solution to your problem

    HTH

    Ollie Riches

    "Trapulo" <> wrote in message
    news:#...
    > I cannot use a non shared variable in a shared method or property...
    >
    > "Ollie Riches" <> wrote in message
    > news:...
    > > if you remove the Shared keyword then it will only 'live' for the life

    > time
    > > of the request
    > >
    > > e.g.
    > >
    > > Public Class Utils
    > >
    > > Private _FcdDataManagement As FCD.DataManagement
    > >
    > > Public Shared ReadOnly Property FcdDataManagement() As
    > > FCD.DataManagement
    > > Get
    > > If _FcdDataManagement Is Nothing Then
    > > _FcdDataManagement = New FCD.DataManagement
    > > End If
    > >
    > > Return _FcdDataManagement
    > > End Get
    > > End Property
    > >
    > > End Class
    > >
    > > HTH
    > >

    >
    >
     
    Ollie Riches, Jan 26, 2005
    #10
  11. Yes. As far as I know they persist between sessions.

    Eliyahu

    "Trapulo" <> wrote in message
    news:...
    > Interesting. I didn't know this.
    >
    > And what is the lifetime in this context? The variable is disposed only

    when
    > the asp.net application ends?
    >
    >
    > "Eliyahu Goldin" <> wrote in message
    > news:ugUl2#...
    > > This is how it is. Shared (static) variables persist between postbacks.
    > >
    > > Eliyahu
    > >

    >
    >
     
    Eliyahu Goldin, Jan 26, 2005
    #11
  12. =?Utf-8?B?VHJhcHVsbw==?=

    Trapulo Guest

    I'll see.

    Thanks

    "Ollie Riches" <> wrote in message
    news:...
    > you need to look at the 'class factory' set of design patterns these will
    > give you a solution to your problem
    >
    > HTH
    >
    > Ollie Riches
    >
    > "Trapulo" <> wrote in message
    > news:#...
    > > I cannot use a non shared variable in a shared method or property...
    > >
    > > "Ollie Riches" <> wrote in message
    > > news:...
    > > > if you remove the Shared keyword then it will only 'live' for the life

    > > time
    > > > of the request
    > > >
    > > > e.g.
    > > >
    > > > Public Class Utils
    > > >
    > > > Private _FcdDataManagement As FCD.DataManagement
    > > >
    > > > Public Shared ReadOnly Property FcdDataManagement() As
    > > > FCD.DataManagement
    > > > Get
    > > > If _FcdDataManagement Is Nothing Then
    > > > _FcdDataManagement = New FCD.DataManagement
    > > > End If
    > > >
    > > > Return _FcdDataManagement
    > > > End Get
    > > > End Property
    > > >
    > > > End Class
    > > >
    > > > HTH
    > > >

    > >
    > >

    >
    >
     
    Trapulo, Jan 26, 2005
    #12
    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. bucrepus

    Variable Lifetime

    bucrepus, Apr 14, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    949
    bruce barker
    Apr 14, 2004
  2. Bryan Parkoff
    Replies:
    2
    Views:
    6,505
  3. Vagif Abilov
    Replies:
    3
    Views:
    18,258
    Laurent Bugnion
    May 24, 2006
  4. Stuart MacMartin
    Replies:
    5
    Views:
    563
    Victor Bazarov
    Jul 27, 2005
  5. somenath
    Replies:
    5
    Views:
    307
    Eric Sosman
    Dec 24, 2007
Loading...

Share This Page