how to pass the variable between two classes

Discussion in 'iOS Programming' started by srinivas.be88, Aug 27, 2010.

  1. srinivas.be88 macrumors newbie

    Joined:
    Aug 10, 2010
    #1
    hi folks,
    i am new to iPhone programming.i have a one question.that is how to pass the variable defined in one class to another class . these two class didnot use inheritence feature.
    for eg:
    Code:
     class A
     {
         int go;
        // some methods ..
     }
     
    
    class B
    {
       some method
    { 
     i want to use "go" variable in this method . is it possible to access the variable from the class A.
    }
    
    }

    please help me to clear the above problem
     
  2. ranguvar macrumors 6502

    Joined:
    Sep 18, 2009
    #2
    Yes, it's possible. You'll have to decide whether you want to use an instance or a class variable. In the case of an instance variable, you'd write:

    Code:
    @interface SomeClass : NSObject {
    	int someInt;
    }
    - (int)someInt;
    @end
    
    
    @implementation SomeClass
    - (int)someInt {
    	return someInt;
    }
    @end
    and:

    Code:
    @interface AnotherClass : NSObject  {
    }
    @end
    
    
    @implementation AnotherClass
    - (void)someMethod {
    	SomeClass *instance = [[SomeClass alloc] init];
    	NSLog(@"someInt from SomeClass instance: %d", [instance someInt]);
    }
    @end

    If you want a class variable, you'd write something like this:

    Code:
    @interface SomeClass : NSObject  {
    }
    + (int)someInt;
    @end
    
    
    @implementation SomeClass
    + (int)someInt {
    	return 4;
    }
    @end
    and:

    Code:
    @interface AnotherClass : NSObject  {
    }
    @end
    
    @implementation AnotherClass
    - (void)someMethod {
    	NSLog(@"someInt from SomeClass: %d", [SomeClass someInt]);
    }
    @end
    In most cases, you'll probably want to use the instance variable. Also, make sure you read this document thoroughly.
     
  3. srinivas.be88 thread starter macrumors newbie

    Joined:
    Aug 10, 2010
    #3
    reply

    thank you for your reply sir,
    sir, already i had created the object for two classes.
    so i changed the value of variable where defined in one class. and displays that variable in another class..
    for eg:
    Code:
     @interface ClassA
     { 
        int a;
      }
     -(void) change : (int) a;
    @end
    
    
    @implement ClassA
     -(void) change : (int) a
    {
      a=a+10;
    }
    
    @end
    ----------------------------------

    Code:
    @interface ClassB
    {
     }
    -(void) display;
    @end
    
    @implement ClassB
     -(void)display
    {
    //here i want to display the variable defined in ClassA's change method.
    }

    hope you understood this...is it possible to display the variable defined in the ClassA's chang method.
     
  4. ranguvar macrumors 6502

    Joined:
    Sep 18, 2009
    #4
    Hm, I don't understand what you mean. If you say "display", do you mean that you want to display the variable in a UIView, or do you mean that you want to further access and modify the int defined in ClassA? And are you sure that you have understood object orientation?
     
  5. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #5
    Forget doing it with objects.

    Make your variable a global variable that you can access from anywhere.

    After you learn to write programs without cognitive overload, then you can learn how to develop larger projects using reusable objects with cleanly encapsulate instance state.

    Humans have to stumble around like a toddler before running 5k's.
     
  6. fluxforge macrumors member

    Joined:
    Apr 16, 2010
    Location:
    europe
    #6
    nice one!
     
  7. RonC macrumors regular

    Joined:
    Oct 18, 2007
    Location:
    Chicago-area
    #7
    You're combining two different concepts into one thing. I see 2 requirements in your code/description:
    1. Add 10 to a number
    2. Retrieve the current value of a number

    You don't need 2 methods for that, you just need to implement it such that the 1 method does both things.

    In the code you presented, you're almost there. Change the signature of the change method to return an int. Change the code in the implementation to actually return the value.

    Then in the code that calls "change," you get the incremented value back.

    There is an opportunity to improve the design. When I think about these things, modifiers (like the first requirement) are separate concepts from accessors (like the second). I like to make them separate methods - it focuses the code on doing one thing, and makes it easy to compose more complicated actions from simpler ones. For example, if you ever want to get the value WITHOUT incrementing it, then you'd probably be better off creating two methods: one to do the incrementing (which may or may not return the current value) and another to do the fetching of the current value.

    Given the current list of requirements you've implied/stated, this improvement is not necessary.
     

Share This Page