Method accessing another method's local variables

Go To StackoverFlow.com

0

Just a quick newbie question here. I have a method that calculates a value and stores the result in a double variable, this variable is also a local variable to that method.I also have a second method that does a separate calculation but this method needs the result in the first. How can I get the value from the first method while still keeping that variable hidden to the rest of the class? Below is an example of what I'm trying to get at.

-(IBAction)methodA{
double answer;
answer = 2 + 3;
}

-(IBAction)methodB{
double answerTimeTwo;
answerTimeTwo = answer * 2;   //Problem arises here as I cannot access "answer"
}
2012-04-04 03:35
by SNV7
I think you should read Oops concept and C language before starting app development - Dushyant Singh 2012-04-04 03:50
I am familiar with OOP concepts, just not familiar with the objective-c synta - SNV7 2012-04-04 03:57
Here's nothing new in objective-c's syntax. As you know you cannot access local object outside the scope of the declaring method. Anyways you got your answe - Dushyant Singh 2012-04-04 04:02


1

They shouldn't be decorated as actions unless they're the result of a UIControl event.

Do it like this:

- (double)methodA {
    double answer = 2.0 + 3.0;  // don't really need the stack variable, but it's okay
    return answer;
}

- (double)methodB {
    double answerTimesTwo = [self methodA] * 2.0;
    return answerTimesTwo;
}
2012-04-04 03:39
by danh
Ads