Documentation Archive Developer
Search
PATH  Documentation > WebObjects 4.5 > WebObjects Developer's Guide


Table of Contents Previous Section

Accessor Methods

As stated previously, you can access any instance variable within any method declared in the same object. If you need to access a variable in a different object, you must send a message to that object.

Accessor methods are methods that other objects can use to access an object's instance variables. When you declare an instance variable, WebScript automatically defines two accessor methods: one to retrieve the instance variable's value, and one to change the value.

For example, suppose an Application.wos script declared this instance variable, which keeps track of the number of visitors:

id visitorNum;
When WebScript parses this file, it sees this declaration and implicitly defines two methods that work like this:

- visitorNum {
return visitorNum;
}

- setVisitorNum:newValue {
visitorNum = newValue;
}
(You don't see these methods in the script file.) The Main.wos script can access the application's visitorNum variable using these statements:

number = [[self application] visitorNum];
...
[[self application] setVisitorNum:number];
Note: self is a keyword that represents the current object. For more information, see "Reserved Words".

You can also access an instance variable declared in one component script from another component script. This is something you commonly do right before you navigate to a new page, for example:

id anotherPage = [[self application] 
pageWithName:@"Hello"]; 
[anotherPage setNameString:newValue];
The current script uses the statement [anotherPage setNameString:newValue]; to set the value of nameString, which is declared in the page named Hello.

Table of Contents Next Section