PDA

View Full Version : Problem in accessing value of variable of a parent class




iphonejudy
Feb 26, 2009, 05:50 AM
Hi,


I need to access a value of variable of A class in B class.

class A
{

NSString *name;

}

@property (nonatomic, retain) NSString * name;

then i do some calculations in the name.Now name ="hello123"

class B

{

A *aobject=[[Address alloc]init];
NSLog(@"In B class: %@", aobject.name);
}

I got the below message
In B class =null.

how can i get the value "hello123" from A class



caveman_uk
Feb 26, 2009, 06:38 AM
That's because aobject is a brand new instance of the first class and as such has absolutely no knowledge of the initialization you did in the other instance. You need to pass the value another way. You could use a datasource style pattern or pass the original class as a property.

iphonejudy
Feb 26, 2009, 06:50 AM
you mean

to access name as A.name?

dejo
Feb 26, 2009, 10:30 AM
Class A is not a parent (or super) class of Class B unless you have B defined as:
@interface B : A
If you don't, they are just separate classes that happen to need to share data.

iphonejudy
Feb 26, 2009, 11:01 PM
Class A is not a parent (or super) class of Class B unless you have B defined as:
@interface B : A
If you don't, they are just separate classes that happen to need to share data.


Shal i know the exact syntax for that?

dejo
Feb 26, 2009, 11:16 PM
Shal i know the exact syntax for that?
I don't understand your question.

iphonejudy
Feb 26, 2009, 11:32 PM
I need to access the content of a variable from one class to another class.

In document they give the below syntax for sharing variable between classes.

classA aobject=[[classA alloc] init];

[[aobject sharedInstance] variable1];

But if i use the above code,i got null value in variable1.
My problem is ,it doesnot access the value of variable.

dejo
Feb 26, 2009, 11:38 PM
Is there a method in ClassA that sets the new value of variable1? Are you calling that method somewhere between when you init your instance of ClassA (which is, presumably, where variable1 gets set to nil) and you access the value of variable1?

shusseina
Mar 1, 2009, 09:06 AM
A couple things you need to do.

As your code stands, class B is not a subclass of A as dejo has pointed out. However, with the code you have it doesn't really matter if class B is a subclass of class A or not.

I assume you expect the call to NSLog to output the following:

In B class: hello123

Has class A got an initialise method (i.e. an init method)? To get the NSLog output above you probably want to add an init method to class A (i.e. override the init method of class A's superclass). Something like the following will give you the output you require:

- (id) init {
self = [super init];

if (self) {
//I'm sure there are better ways to do this
name = [[NSString alloc] initWithString:@"hello123"];
..
}
...
return self;
}

Couple of other points...

I'm sure someone else can tell us how primitive types and objects of a class are initialised in Objective-C if they are not explicitly initialised in the init method of a class.

Generally you should define your classes in the following way (if they are not a subclass of some other class):

class A : NSObject {
...

This way, they inherit from the root class NSObject. The way you have coded it, class A is defined as another root class.

Hope the above helps.