How to cast objects from one class to another.
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server | |
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.
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(); }
getSelectedRowCursor()
.
getProperty("employeeid")
on the cursor that was just obtained. The argument employeeid is the column containing the desired data. The code casts the resulting Object to an Integer object by using (Integer)
.
intValue()
on the Integer object. This is necessary because fldID.setValue()
takes an int, which is a primitive data type.
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(); }
getSelectedRowCursor()
.
getProperty("name")
on the cursor that was just obtained. The argument name is the column containing the desired data. The code casts the resulting Object to a String object by using (String)
.
setText()
.
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(); }
getSelectedRowCursor()
.
getProperty("hiredate")
on the cursor that was just obtained. The argument hiredate is the column containing the desired data. The code casts the resulting Object to a Date object by using (java.sql.Date)
.
setValue()
.