karlhsc
asked on
Class Level Variable Issue
Hello,
I have a class level variable declared and set to 0 at the top of my class. There is a certain method in the class that changes the value of this variable from 0 to another number.
After this method fires, this variable is still set to 0, though it should be 3. I setup a debugging breakpoint to watch what happens within the method and the line of code that is supposed to change the value of this class variable does its job and the value does change. However, the moment flow finishes with this method, and returns, the value of the class variable returns to 0.
Any ideas?
Thank you in advance.
I have a class level variable declared and set to 0 at the top of my class. There is a certain method in the class that changes the value of this variable from 0 to another number.
After this method fires, this variable is still set to 0, though it should be 3. I setup a debugging breakpoint to watch what happens within the method and the line of code that is supposed to change the value of this class variable does its job and the value does change. However, the moment flow finishes with this method, and returns, the value of the class variable returns to 0.
Any ideas?
Thank you in advance.
Can we see some code?...
ASKER
public partial class MyClass : PhoneApplicationPage
{
int midpointMinutesTotal = 0;
public MyClass()
{
FindMidpointInTimer();
}
public void FindMidpointInTimer()
{
//find total minutes
int originalTimerMinutesTotal = (Int32.Parse(OriginalHoursTextBlock.Text) * 60) + Int32.Parse(OriginalMinutesTextBlock.Text);
int midpointMinutesTotal = (int)(originalTimerMinutesTotal / 2);
}
}
public partial class MyClass : PhoneApplicationPage
{
int midpointMinutesTotal = 0;
public MyClass()
{
FindMidpointInTimer();
}
public void FindMidpointInTimer()
{
//find total minutes
int originalTimerMinutesTotal = (Int32.Parse(OriginalHoursTextBlock.Text) * 60) + Int32.Parse(OriginalMinutesTextBlock.Text);
int midpointMinutesTotal = (int)(originalTimerMinutesTotal / 2);
}
}
That is because you are not holding onto the instance of that class that has the changed value. All new instances of the class will have the variable set back to ZERO.
MyClass awesomeClass = new MyClass();
awesomeClass.changeNumberV alueToThre e();
awesomeClass.getNumberValu e(); (This will be 3 cause its the same instance)
MyClass anotherClass = new MyClass();
anotherClass.getNumberValu e(); (this will be 0 cause its a different instance)
If that makes no sense can you paste you code, and your calling code?
MyClass awesomeClass = new MyClass();
awesomeClass.changeNumberV
awesomeClass.getNumberValu
MyClass anotherClass = new MyClass();
anotherClass.getNumberValu
If that makes no sense can you paste you code, and your calling code?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
On line 18, the value of midpointMinutesTotal changes, but after flow returns to MyClass (from the FindMidpointInTimer method), the value goes back to 0;
If you want to avoid this type of error in the future, get into the habit of prefacing class level variables with "this.":
this.midpointMinutesTotal = (int)(originalTimerMinutes Total / 2);
Now it is clear that "midpointMinutesTotal" is a class level variable, and the correct variable will be accessed even if you accidentally declared a local variable with the same name.
this.midpointMinutesTotal = (int)(originalTimerMinutes
Now it is clear that "midpointMinutesTotal" is a class level variable, and the correct variable will be accessed even if you accidentally declared a local variable with the same name.