Global variable lifetime

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 
Ollie Riches said:
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
 
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
 
If you want "lifetime of a single page's execution", why do you make it
Shared?

Eliyahu
 
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.
 
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?
 
you need to look at the 'class factory' set of design patterns these will
give you a solution to your problem

HTH

Ollie Riches
 
Back
Top