Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,I made a desktop application and using a global variable for counting the number of clicks on a button.
but when i reset the global variable its not reset.
i used global variable as
C#
int i=0;



and i am writing this at reset button

C#
int i=0;


Is i am wrong to reset global variable.

Please help me out.

I am new in coding.

Thanks In Advance.
Posted

C# has no global variables. Possibly do you mean a class member variable?

In the reset button you are declaring (and initializing) a new instance of the variable and thus shadowing the (possible) class member variable.

You have to change (in the reset button command handler) from:

C#
int i = 0;


to

i = 0;
 
Share this answer
 
Comments
mayankshrivastava 31-Jan-12 7:58am    
thanks...
CPallini 31-Jan-12 7:59am    
You are welcome.
TimGameDev 31-Jan-12 8:38am    
Simple as it is. My 5
thatraja 31-Jan-12 9:42am    
Yep, I think he meant static variable.
Spot on, 5!
Sergey Alexandrovich Kryukov 31-Jan-12 17:59pm    
Right, a 5.
--SA
write on reset button only
C#
i=0;
 
Share this answer
 
v2
Comments
mayankshrivastava 31-Jan-12 7:58am    
thanks...
TimGameDev 31-Jan-12 8:38am    
My 4. There is no note that that OP has mistake in his question about global variables in C#.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900