Click here to Skip to main content
14,669,375 members
Rate this:
Please Sign up or sign in to vote.
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
int i=0;



and i am writing this at reset button

int i=0;


Is i am wrong to reset global variable.

Please help me out.

I am new in coding.

Thanks In Advance.
Posted

Rate this:
Please Sign up or sign in to vote.

Solution 2

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:

int i = 0;


to

i = 0;
   
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
Tech Code Freak 1-Feb-12 3:39am
   
5up!
Rate this:
Please Sign up or sign in to vote.

Solution 1

write on reset button only
i=0;
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100