Defining a Valid Object
In the DB Interface Module, a valid object is a usable object, so we often apply the terms interchangeably. The function isValid() returns true if the object is valid, as shown in the next example:
 
RWDBConnection aConnection = goodDB.connection(); // 1
if (aConnection.isValid()) { // 2
cout << “A good connection was established.” << endl;
}
In //1, a valid RWDBDatabase produces a valid RWDBConnection. Because the connection is successfully established, the function RWDBConnection::isValid() on line //2 returns true. The connection is valid and usable.
If an object that provides an isValid() member is not in a usable state, its isValid() method returns false, as shown in the following example:
 
RWDBConnection aConnection = goodDB.connection(); // 1
RWDBResult aResult =
aConnection.executeSql(“Some Bad SQL”); // 2
if(aResult.isValid()) { // 3
cout << “RWDBResult object is valid.” << endl;
}
else{
cout << “RWDBResult object is not valid--bad SQL statement?”
<< endl;
}
if(aConnection.isValid()) { // 4
cout << “The connection is still good.” << endl;
}
else{
cout << “The connection is bad. Why?” << endl;
}
On line //1, a connection is established from a valid RWDBDatabase. Since the connection is successfully established, line //2 sends the string “Some Bad SQL” to the database server. Of course, this is an invalid query, so the function RWDBResult::isValid() returns false on //3 because this erroneous query is unusable. On line //4, however, aConnection.Valid() returns true, since the RWDBConnection instance is still valid; the connection is still usable for other queries.