Documentation Archive Developer
Search
Table of Contents Previous Section

Create variables

Now you have a form in which guests can enter information. You need to create variables that store this information. You need two variables: one to store the current guest's input, and another to store the list of all guests. You'll begin by declaring a class for these two variables.

Create a class

The form you just created contains three pieces of information from a guest-name, e-mail, and comments. You could create three separate variables for these three pieces of information, but as you will see later, creating a class that has three attributes makes writing the application easier by allowing you to treat these three separate pieces as a single entity-a guest.

WebObjects Builder recognizes three types of classes: base classes, such as numbers and strings, composite classes, such as arrays and dictionaries, and custom classes. You don't need to do anything special to create variables of a base class or an array class. But if you want to create a dictionary, you must first go to the Classes window and define the dictionary.

In this task, you will create a dictionary class named Guest. The Guest dictionary class has three keys: name, email, and comments. (You can think of this dictionary as a C structure with three fields.)

  1. Go to the application window and click the Application tab.

    The application object browser appears. You use this browser to declare variables that have an application-wide scope. These variables are declared in the application script, which is an optional part of a WebObjects application.

  2. Choose ToolsClasses to display the Classes window. This window lists all of the classes that your application recognizes.

  3. Click the plus sign in the Classes table to create a new class.

  4. Name the class Guest. The Guest class is created with two default attributes.

  5. Rename the default attributes name and email.

  6. Click the plus sign in the Attribute table view to add a third attribute named comments.

  7. Close the Classes window.

As stated previously, Guest is a dictionary class. A dictionary contains key-value pairs. In the class definition you just created, you specified the keys (or attributes): name, email, and comments. When you create a variable of the Guest class, the variable will store values for each of these three keys.

Declare variables

You just defined a Guest class so that you can store and manipulate guest information as a single entity. The next step is to create the array that will store the list of guests and a variable that will store the current guest information. As explained later, these variables are declared in two separate parts of the application: the list is declared in the application script, and the current guest is declared in the Main component.

  1. Go back to the application window and click the add variable button.

  2. Type guests in the Name field and press Enter.

  3. Choose Guest from the Class pop-up list.

  4. Click the Array check box.

    This creates the application variable guests, which is an array of Guests.

    .

  5. Choose FileSave to save your changes to the application script.

  6. In the Main window, click the add variable button (in the bottom half of the screen).

  7. Name the variable aGuest and select Guest from the Class pop-up list.

  8. Save the Main component.

A closer look

You just created the variables that represent the current user's input (aGuest) and the list of all guests (guests).

You declared the guests array in the application script (which is named Application.wos) instead of the Main component's script. Why? So that it would exist for the life of the application. If you declared guests in the Main component (as you did the aGuest variable), guests would be created each time the Main page was redrawn. The Main page is redrawn every time a user clicks the Submit button, so guests would only ever contain one item - the last user who clicked submit.

Note: Component variables exist only as long as the component does, which is up until that component's page needs to be redrawn. Application variables (defined in Application.wos) live as long as the application does. There's a third type of variable, known as session variables. Session variables are declared in the session display of the application window and stored in the session script (Session.wos). They exist for the lifetime of one user's session. New session variables are created as new sessions are added. For example, if you declared a variable in Session.wos and three users were using the application, three instances of that variable would be created, one for each user. If the first user changed the value of that variable, the other two users would not see the change because the session variable is unique to each user's session.

You can read more about application variable, session variables, and component variables in "Using WebScript" in the WebObjects Developer's Guide.

Table of Contents Next Section