Application Techniques



Casting

How to cast objects from one class to another.

About this technique

Details

Category

Core Programming Techniques> Java

Description

You'll learn about:

You can run this technique code from:

NOTE   First make sure that database is running on your localhost SilverStream Server

Related reading

See the chapter on coding Java for SilverStream applications in the Programmer's Guide

Many SilverStream methods return a Java Object which you must then cast to the appropriate class. This example uses the AgiRowCursor getProperty() method to demonstrate.

Casting an Object to an Integer   Top of page

The following code illustrates how to take an Object obtained from a view and cast it to an Integer object. The result is then displayed in another field on the form. As that display field receives the focus, the field's name and class are displayed for informational purposes.

  // Clicked event for the button tbGetInteger that has the 
  // caption "Get Integer" 
  // Get a cursor for the currently-selected row in the view 
  AgiRowCursor cursor = vwCasting.getSelectedRowCursor(); 
   
  // Got a cursor? 
  if (cursor != null) 
  { 
       // Yes, so get the value from the Employee ID column 
       Integer id = (Integer) cursor.getProperty("employeeid"); 
   
       // Display the Employee ID in fldID 
       fldID.setValue(id.intValue()); 
   
       // Set the focus to fldID so that the Control and Class 
       // labels are updated 
       fldID.requestFocus(); 
  } 

Notes about the code

Casting an Object to a String   Top of page

The following code illustrates how to take an Object obtained from a view and cast it to a String object. The String is then displayed in another field on the form. As that display field receives the focus, the field's name and class are displayed for informational purposes.

  // Clicked event for the button tbGetString that has the 
  // caption "Get String" 
  // Get a cursor for the currently-selected row in the view 
  AgiRowCursor cursor = vwCasting.getSelectedRowCursor(); 
   
  // Got a cursor? 
  if (cursor != null) 
  { 
       // Yes, so get the value from the Employee Name column 
       String name = (String) cursor.getProperty("name"); 
   
       // Display the Employee Name in fldName 
       fldName.setText(name); 
   
       // Set the focus to fldName so that the Control and Class 
       // labels are updated 
       fldName.requestFocus(); 
  } 

Notes about the code

Casting an Object to a Date   Top of page

The following code illustrates how to take an Object obtained from a view and cast it to a Date object. The date is then displayed in another field on the form. As that display field receives the focus, the field's name and class are displayed for informational purposes.

  // Clicked event for the button tbGetDate that has the 
  // caption "Get Date" 
  // Get a cursor for the currently-selected row in the view 
  AgiRowCursor cursor = vwCasting.getSelectedRowCursor(); 
   
  // Got a cursor? 
  if (cursor != null) 
  { 
       // Yes, so get the value from the Hire Date column 
       java.sql.Date hiredate = (java.sql.Date) 
         cursor.getProperty("hiredate"); 
   
       // Display the Hire Date in fldHireDate 
       fldHireDate.setValue(hiredate); 
   
       // Set the focus to fldHireDate so that the Control and 
       // Class labels are updated 
       fldHireDate.requestFocus(); 
  } 

Notes about the code






Copyright © 2000, SilverStream Software, Inc. All rights reserved.