4. System Classes : StringHashTable Class : StringHashTable Examples
 
Share this page                  
StringHashTable Examples
In the following examples, an object of class StringHashTable is used to store employee objects based on the department they work in. Another StringHashTable table is used to store the same employees based on their knowledge of languages.
Set up the hash table with about 100 slots:
DECLARE
    dept_hash = StringHashTable;
    lang_hash = StringHashTable;
ENDDECLARE
{
    dept_hash.Setup(ignorecase=TRUE);
    lang_hash.Setup(ignorecase=TRUE);
}
Retrieve employees from the database and insert them into the hash table objects:
DECLARE
    emp  = Employee;
    lang = VARCHAR(20) NOT NULL;
ENDDECLARE
{
    SELECT id AS :emp_id,
        name AS :emp.name,
        title AS :emp.first_name,
        dept AS :emp.dept
    FROM employee
    BEGIN
        dept_hash.InsertObject(key=emp.dept, object=emp);
 
    SELECT lang AS :lang
        FROM emp_lang
        WHERE emp_id = :emp.id
        BEGIN
            lang_hash.InsertObject(key=lang, object=emp);
        END;
        emp = Employee.Create();
    END;
    COMMIT;
}
Get objects from the hash table. Show employees who belong to a department in the TableField emp_tf.
ON SETVALUE dept =
{
    emp_tf.Clear();
    dept_hash.FindAll(key=dept, results = emp_tf);
    FIELD(emp_tf).UpdField();
}
Show employees who speak a selected language in the TableField emp_tf.
ON SETVALUE lang_opt =
{
    emp_tf.Clear();
    lang_hash.FindAll(key= lang_opt, results = emp_tf);
    FIELD(emp_tf).UpdField();
}
Remove all employees of a certain department from the hash table:
    cust_hash.RemoveObject(key=dept, allobjects=TRUE,
        removedcount=BYREF(remno));