Link to home
Start Free TrialLog in
Avatar of karlhsc
karlhscFlag for Afghanistan

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.
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Can we see some code?...
Avatar of karlhsc

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

        }

}

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

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?
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of karlhsc

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)(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.