Table of Contents Previous Section

Variables

To declare a variable in WebScript, use the syntax:

	id myVar;
	id myVar1, myVar2;
In these declarations, id is a data type. The id type is a reference to any object-in reality, a pointer to the object's data (its instance variables). Like a C function or an array, an object is identified by its address; thus, all variables declared in WebScript are pointers to objects. In the examples above, myVar1 and myVar2 could be any object: a string, an array, or a custom object from your application.

Note: Unlike C, no pointer manipulation is allowed in WebScript.

Instead of using id, you can specifically refer to the class you want to instantiate using this syntax:

	className *variableName;
For example, you could specify that a variable is an NSString object using this syntax:

	NSString *myString1;
	NSString *myString1, *myString2;
For more information on specifying class names in variable declarations, see the section "Data Types".

In WebScript, there are two basic kinds of variables: local variables and instance variables. You declare instance variables at the top of the file, and you declare local variables at the beginning of a method or at the beginning of a block construct (such as a while loop). The following shows where variables can be declared:

	id instanceVariable; // An instance variable for this class.

	- aMethod {
		id localVariable1; // A local variable for this method.

		while (1) {
			NSString *localVariable2; // A local variable for this block.
		}
	}

Variables and Scope

Each kind of variable has a different scope and a different lifetime. Local variables are only visible inside the block of text in which they are declared. In the example above, localVariable1 is declared at the top of a method. It is accessible within the entire body of that method, including the while loop. It is created upon entry into the method and released upon exit. localVariable2, on the other hand, is declared in the while loop construct. You can only access it within the curly braces for the while loop, not within the rest of the method.

The scope of an instance variable is object-wide. That means that any method in the object can access any instance variable. You can't directly access an instance variable outside of the object that owns it; you must use an accessor method instead. See "Accessor Methods".

The lifetime of an instance variable is the same as the lifetime of the object. When the object is created, all of its instance variables are created as well and their values persist throughout the life of the object. Instance variables are not freed until the object is freed.

As you learned in the chapter "Common Methods", a WOApplication is created when you started a WebObjects application, a WOSession is created each time a different user accesses that application, and a WOComponent is created the first time a user accesses that page in the application. Thus, the variables you declare at the top of the application script (Application.wos) exist as long as the application is running. The variables you declare at the top of the session script (Session.wos) exist for the length of one session. As new users access your application, new sessions are created, so new copies of the session's instance variables are created too. These copies of instance variables are private to each session; one session does not know about the instance variables of another session. As sessions expire, their instance variables are freed. Finally, the variables you declare at the top of a component script are created and released as that component is created and released.

Note: Just how often a particular component object is created depends on whether the application object is caching pages. For more information, see "WebObjects Viewed Through Its Classes".

Assigning Values to Variables

You assign values to variables using the following syntax:

	myVar = aValue;
A value can be assigned to a variable at the time it is declared or after it is declared. For example:

	NSNumber *myVar1;
	id myVar2 = 77;

	myVar1 = 76; 
The value you assign to a variable can be either a constant or another variable. For example:

	// assign another variable to a variable
	myVar = anotherVar;
	// assign a string constant to a variable
	myString = @"This is my string.";
Note: The // syntax denotes a comment.

You can assign constant values to objects of four of the most commonly used classes in WebScript: NSNumber, NSString, NSArray, and NSDictionary. These classes are defined in the Foundation framework. To learn how to initialize objects of all other classes, see "Creating Instances of Classes" in this chapter.

NSNumber is the easiest class to initialize. You just assign a number to the variable, like this:

	NSNumber *myNumber = 77;
For the remaining three classes, WebScript provides a convenient syntax for initializing constant objects. In such an assignment statement, the value you're assigning to the constant object is preceded by an at sign (@). You use parentheses to enclose the elements of an NSArray and curly braces to enclose the key-value pairs of an NSDictionary. The following are examples of how you use this syntax to assign values to constant NSString, NSArray, and NSDictionary objects in WebScript:

	myString = @"hello world";
	myArray = @("hello", "goodbye");
	myDictionary = @{"key" = 16};
	anotherArray = @(1, 2, 3, "hello");
	aDict = @{ "a" = 1; "b" = "hello world"; "c" = (1,2,3); 
			"d" = { "x" = 1; "r" = 2 }};
The following rules apply when you use this syntax to create constant objects:

For more information on NSNumber, NSString, NSDictionary, and NSArray, see the chapter "WebScript Programmer's Quick Reference to Foundation Classes".

Table of Contents Next Section