# Using a Range of View Object Rows in Code

When you do not want to work with an entire result set, you can use a range to specify how many rows to fetch. A range effectively defines a window you can use to access a subset of the rows in a result set. Ranges are useful when the result set is large and you do not want to bring all of the rows to the client, or when you want to display a certain number of rows in a control. To define a range, specify a range size (number of rows) and the index of the first row in the range. By default, the range size is set to 1 (note: a range size of -1 fetches all rows). To work with a range, use a View Object to call the methods below (range functionality is defined in the `oracle.jbo.RowIterator` interface).

`enumerateRowsInRange`
`getAllRowsInRange`
`getRangeIndexOf`
`getRangeSize`
`getRangeStart`
`getRowCountInRange`
`insertRowAtRangeIndex`
`scrollRange`
`scrollRangeTo`
`setCurrentRowAtRangeIndex`
`setRangeSize`
`setRangeStart`

The following figure shows the effects of some of these methods. In the view at left, the range size is set to 5 (`setRangeSize(5)`), so the range contains the first 5 rows of the result set. Next, in the middle view, the start of the range is at row 4 (range start is 0-based, thus `setRangeStart(3)`) of the result set and the range size is set to 3, so the range contains rows 4 through 6 of the result set. Finally, in the view at right, the range window is scrolled forward 4 rows. The range size does not change, so the range contains rows 8 through 10 of the result set.

The following code example shows how to use some of these methods to work with ranges. The example assumes that a helper routine to initialize an Application Module has been implemented elsewhere.

    package d2e;
    import oracle.jbo.*;

    public class RangeDemo {
      public static void main(String[] args) {

        // Helper routine connects to the generic Application Module.
        ApplicationModule appMod =
           QueryDemo.getGenericAppMod(JboContext.PLATFORM_LOCAL);

        // Specify the Java file that defines the View Object.
        // Format: <package>.<filename>
        String voDefFile = "d2e.EmpView";

        // Identify the View Object. Must be a valid Java identifier.
        String voName = "demoEmpVO";

        // Create the View Object within the context defined
        // by the Application Module.
        ViewObject vo = appMod.createViewObject(voName, voDefFile);

        // Specify how many rows to fetch, then fetch them.
        vo.setRangeSize(5); // vo.setRangeSize(-1) fetches all rows.
        printRows(vo.getAllRowsInRange(), vo.getRangeStart()); // Gets rows 0 - 4.

        // Range window starts at row 5. Row numbers start with 0.
        vo.setRangeStart(4);
        vo.setRangeSize(3); // Change range size.
        printRows(vo.getAllRowsInRange(), vo.getRangeStart()); // Gets rows 4 - 6.

        // Range window scrolls 2 rows. Range size stays the same.
        vo.scrollRange(-2); // Positive scrolls forward, negative scrolls back.
        printRows(vo.getAllRowsInRange(), vo.getRangeStart()); // Gets rows 2 - 4.
     }

     // This is a helper method that prints data to the screen.
      public static void printRows(Row[] rows, int rnum) {
        for (int r = 0; r < rows.length; r++) {
          String rowAttrs = "";
          for (int i = 0; i < rows[r].getAttributeCount(); i++) {
            rowAttrs += (rows[r].getAttribute(i) + "\t");
          }

          System.out.println(rnum + "\t" + rowAttrs);
          rnum += 1;
        }

        System.out.println("----");
      }
    }