NetRexx Overview, version 2.05 Copyright (c) IBM Corporation, 2005. All rights reserved. © | 14 Jan 2005
[previous | contents | next]

## Things that aren’t strings

In all the examples so far, the data being manipulated (numbers, words, and so on) were expressed as a string of characters. Many things, however, can be expressed more easily in some other way, so NetRexx allows variables to refer to other collections of data, which are known as objects.

Objects are defined by a name that lets NetRexx determine the data and methods that are associated with the object. This name identifies the type of the object, and is usually called the class of the object.

For example, an object of class Oblong might represent an oblong to be manipulated and displayed. The oblong could be defined by two values: its width and its height. These values are called the properties of the Oblong class.

Most methods associated with an object perform operations on the object; for example a `size` method might be provided to change the size of an Oblong object. Other methods are used to construct objects (just as for arrays, an object must be constructed before it can be used). In NetRexx and Java, these constructor methods always have the same name as the class of object that they build (‘Oblong’, in this case).

Here’s how an Oblong class might be written in NetRexx (by convention, this would be written in a file called `Oblong.nrx`; implementations often expect the name of the file to match the name of the class inside it):

      /* Oblong.nrx -- simple oblong class */
      class Oblong
        width       -- size (X dimension)
        height      -- size (Y dimension)

        /* Constructor method to make a new oblong */
        method Oblong(newwidth, newheight)
          -- when we get here, a new (uninitialized) object
          -- has been created.  Copy the parameters we have
          -- been given to the properties of the object:
          width=newwidth; height=newheight

        /* Change the size of an Oblong */
        method size(newwidth, newheight) returns Oblong
          width=newwidth; height=newheight
          return this   -- return the resized object

        /* Change the size of an Oblong, relatively */
        method relsize(relwidth, relheight)-
                      returns Oblong
          width=width+relwidth; height=height+relheight
          return this

        /* ’Print’ what we know about the oblong */
        method print
          say ’Oblong’ width ’x’ height

To summarize:

1. A class is started by the class instruction, which names the class.
2. The class instruction is followed by a list of the properties of the object. These can be assigned initial values, if required.
3. The properties are followed by the methods of the object. Each method is introduced by a method instruction which names the method and describes the arguments that must be supplied to the method. The body of the method is ended by the next method instruction (or by the end of the file).

The `Oblong.nrx` file is compiled just like any other NetRexx program, and should create a class file called `Oblong.class`. Here’s a program to try out the Oblong class:

      /* tryOblong.nrx -- try the Oblong class */

      first=Oblong(5,3)        -- make an oblong
      first.print              -- show it
      first.relsize(1,1).print -- enlarge and print again

      second=Oblong(1,2)       -- make another oblong
      second.print             -- and print it

when `tryOblong.nrx` is compiled, you’ll notice (if your compiler makes a cross-reference listing available) that the variables `first` and `second` have type `Oblong`. These variables refer to Oblongs, just as the variables in earlier examples referred to NetRexx strings.

Once a variable has been assigned a type, it can only refer to objects of that type. This helps avoid errors where a variable refers to an object that it wasn’t meant to.

#### Programs are classes, too

It’s worth pointing out, here, that all the example programs in this overview are in fact classes (you may have noticed that compiling them with the reference implementation creates `xxx.class` files, where `xxx` is the name of the source file). The environment underlying the implementation will allow a class to run as a stand-alone application if it has a static method called `main` which takes an array of strings as its argument.

If necessary (that is, if there is no class instruction) NetRexx automatically adds the necessary class and method instructions for a stand-alone application, and also an instruction to convert the array of strings (each of which holds one word from the command string) to a single NetRexx string.

The automatic additions can also be included explicitly; the ‘toast’ example could therefore have been written:

      /* This wishes you the best of health. */
      class toast
        method main(argwords=String[]) static
          arg=Rexx(argwords)
          say ’Cheers!’

though in this program the argument string, `arg`, is not used.

[previous | contents | next]

From The NetRexx Language by Mike Cowlishaw, mfc@uk.ibm.com (ISBN 0-13-806332-X, 197pp, Prentice-Hall, 1997).