473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Global variable lifetime

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

Nov 19 '05 #1
11 1714
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" <no**********@qui.it> wrote in message
news:2B**********************************@microsof t.com...
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

Nov 19 '05 #2
This is how it is. Shared (static) variables persist between postbacks.

Eliyahu

"Trapulo" <no**********@qui.it> wrote in message
news:2B**********************************@microsof t.com...
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

Nov 19 '05 #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
Nov 19 '05 #4
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" <no**********@qui.it> wrote in message
news:FB**********************************@microsof t.com...
"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

Nov 19 '05 #5
If you want "lifetime of a single page's execution", why do you make it
Shared?

Eliyahu

"Trapulo" <no**********@qui.it> wrote in message
news:FB**********************************@microsof t.com...
"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

Nov 19 '05 #6
I cannot use a non shared variable in a shared method or property...

"Ollie Riches" <ol**********@hotmail.com> wrote in message
news:ug**************@TK2MSFTNGP12.phx.gbl...
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

Nov 19 '05 #7
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" <re*************@monarchmed.com> wrote in message
news:OA**************@TK2MSFTNGP11.phx.gbl...
If you want "lifetime of a single page's execution", why do you make it
Shared?

Eliyahu

Nov 19 '05 #8
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" <re*************@monarchmed.com> wrote in message
news:ug*************@TK2MSFTNGP12.phx.gbl...
This is how it is. Shared (static) variables persist between postbacks.

Eliyahu

Nov 19 '05 #9
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" <no**********@qui.it> wrote in message
news:#X*************@TK2MSFTNGP09.phx.gbl...
I cannot use a non shared variable in a shared method or property...

"Ollie Riches" <ol**********@hotmail.com> wrote in message
news:ug**************@TK2MSFTNGP12.phx.gbl...
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


Nov 19 '05 #10
Yes. As far as I know they persist between sessions.

Eliyahu

"Trapulo" <no**********@qui.it> wrote in message
news:uQ**************@TK2MSFTNGP14.phx.gbl...
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" <re*************@monarchmed.com> wrote in message
news:ug*************@TK2MSFTNGP12.phx.gbl...
This is how it is. Shared (static) variables persist between postbacks.

Eliyahu


Nov 19 '05 #11
I'll see.

Thanks

"Ollie Riches" <ol**********@hotmail.com> wrote in message
news:O0**************@TK2MSFTNGP14.phx.gbl...
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" <no**********@qui.it> wrote in message
news:#X*************@TK2MSFTNGP09.phx.gbl...
I cannot use a non shared variable in a shared method or property...

"Ollie Riches" <ol**********@hotmail.com> wrote in message
news:ug**************@TK2MSFTNGP12.phx.gbl...
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



Nov 19 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: | last post by:
In global.asa I have some code outside all the Subs. I have some confirmation that it is being executed OnStart. Yet I can's see if it is executed OnEnd. The literature I have says that OnEnd...
3
by: JJ | last post by:
Hi All, I was wondering if I create an object in public main does it persist for the lifetime of the app? If not where do u create objects that will persist for the lifetime of app so that when...
10
by: Simon Harvey | last post by:
Hi everyone, Can anyone tell me if I declare a global variable in my pages code behind, is it persisted if the page does a post back, or do I need to add the object to the session object in...
41
by: Miguel Dias Moura | last post by:
Hello, I am working on an ASP.NET / VB page and I created a variable "query": Sub Page_Load(sender As Object, e As System.EventArgs) Dim query as String = String.Empty ... query =...
2
by: Nathan Sokalski | last post by:
I would like to access variables and functions that I declare in the Global.asax.vb file. However, I am having trouble doing that. What does the declaration have to look like in the Global.asax.vb...
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
10
by: Yehia A.Salam | last post by:
Hello, I was trying to connect to an Xml database, and I thought of loading the Xml Document in "Application_Start" so that the xml is loaded only once and then queried later as many times as...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
6
by: Rajesh | last post by:
I read Global Object's constructor will be called before main() function; In which situation it can be really helpful? Is it good practice use Global object and its constructor ? Thanks,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.