Class Level Variable Issue

karlhsc
karlhsc used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Can we see some code?...

Author

Commented:
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);

        }

}

Open in new window

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);

        }

}

Open in new window

Commented:
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.changeNumberValueToThree();
awesomeClass.getNumberValue();  (This will be 3 cause its the same instance)

MyClass anotherClass = new MyClass();
anotherClass.getNumberValue(); (this will be 0 cause its a different instance)

If that makes no sense can you paste you code, and your calling code?
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
At line #18, you've declared "midpointMinutesTotal" as a LOCAL variable, which HIDES the class level variable.

Change this:

            int midpointMinutesTotal = (int)(originalTimerMinutesTotal / 2);

To:

            midpointMinutesTotal = (int)(originalTimerMinutesTotal / 2);

*Note the removal of "int" at the beginning of the line!

Author

Commented:
On line 18, the value of midpointMinutesTotal changes, but after flow returns to MyClass (from the FindMidpointInTimer method), the value goes back to 0;
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
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)(originalTimerMinutesTotal / 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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial