difference between static and global variable in .net

Posted by Shanky11 under ASP.NET on 9/21/2013 | Points: 10 | Views : 2681 | Status : [Member] | Replies : 5
ihave created a static vaieable and store it i n viewstate on page load and conter ++
but the problem is when i exit the web app it agains starts formwhre it left last time
thats why i am trying to use gobal variable int counter=0
but it is not incresing
what can i do for this??????




Responses

Posted by: Bandi on: 9/21/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
https://forum.lowyat.net/topic/372476/all?_e_pi_=7%2CPAGE_ID10%2C3992542384

http://forums.asp.net/t/517866.aspx
http://stackoverflow.com/questions/1548421/incrementing-variables-in-asp-net-on-button-click
http://www.sitepoint.com/forums/showthread.php?471182-incrementing-a-variable-by-1-when-a-button-is-pressed!



Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nismeh on: 9/23/2013 [Member] Starter | Points: 25

Up
0
Down
http://forums.asp.net/t/489742.aspx
http://social.msdn.microsoft.com/Forums/vstudio/en-US/15b55a9f-ee92-495a-a18c-9f5dc42cca96/difference-between-global-extern-global-static-static-global

IT KNOWLEDGE IS APPLIED KNOWLEDGE
So Just Do It

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/23/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi Shanky,
Refer this link for your scenario...
http://www.sitepoint.com/forums/showthread.php?471182-incrementing-a-variable-by-1-when-a-button-is-pressed!

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/23/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
1. Defines a global variable, allocating memory for it which will be accessible by all files in the program.
2. Declares a global variable which is defined in another file. No memory is allocated. This is how global variables defined in one file can be used in another.
3. static local variables retain their values on repeated calls into the function unlike other local variables.
4. These global variables can only be accessed from within the file. Using the static keyword you can declare global variables with the same name in multiple files in the same program.

Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/23/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
1. Global variable (example : int i_Global)
Defines a global variable, allocating memory for it which will be accessible by all files in the program.

2. Extern variable (example : extern int i_Global)
Declares a global variable which is defined in another file. No memory is allocated. This is how global variables defined in one file can be used in another.

3. static variable (example : static int i_Local means in .h file)
static local variables retain their values on repeated calls into the function unlike other local variables.

4. static global variable ( example : static int i_Global)
These global variables can only be accessed from within the file. Using the static keyword you can declare global variables with the same name in multiple files in the same program.


Static Variables

1. Variables defined local to a function disappear at the end of the function scope. So when we call the function again, storage for variables is created and values are reinitialized.

2. So if we want the value to be extent throughout the life of a program, we can define the local variable as "static." Initialization is performed only at the first call and data is retained between func calls.

3. Static variables are local in scope to their module in which they are defined, but life is throughout the program. Say for a static variable inside a function cannot be called from outside the function but is alive and exists in memory.

Application Variables
1. Global Variables are also similar to the static variables but these variables are available outside the module/function and its scope is available through out the application.

2. These Variables can be accessed from any of the function


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response