Global variable lifetime

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
 
O

Ollie Riches

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
 
G

Guest

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
 
E

Eliyahu Goldin

This is how it is. Shared (static) variables persist between postbacks.

Eliyahu
 
O

Ollie Riches

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
 
E

Eliyahu Goldin

If you want "lifetime of a single page's execution", why do you make it
Shared?

Eliyahu
 
T

Trapulo

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.
 
T

Trapulo

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?
 
O

Ollie Riches

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

HTH

Ollie Riches
 
T

Trapulo

I'll see.

Thanks

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

HTH

Ollie Riches
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top