How Can We Help?
< All Topics
Print

Working with Sets in ZIM

Understanding Result Set References in Zim

Introduction: Result set references in Zim are crucial for managing and manipulating data efficiently. This guide provides detailed examples to illustrate how result set references are compiled and highlights situations to avoid for optimal performance.


Example 1: Basic Result Set Creation

procedure test()
   find all Employees -> EmpSet
   ... other non-FIND commands ...
   find all Departments -> DeptSet
   ... other non-FIND commands ...
endprocedure
  • Explanation: This procedure creates two result sets, EmpSet and DeptSet. The compiler assumes references to the current set are to EmpSet until the second FIND command, after which references apply to DeptSet.

Technical Insight:

  • Compiler Behavior: The compiler processes commands sequentially. After the first FIND command, any reference to the current set points to EmpSet. After the second FIND, references switch to DeptSet.

Example 2: Conditional Result Set Creation

procedure GetEmps (DeptNo, NewDeptNo)
   if DeptNo = 'D01'
      find all Employees where DeptNum = DeptNo
   else
      find all Departments where DeptNum = DeptNo
   endif
   change all CurrentSet let DeptNum = NewDeptNo
endprocedure
  • Explanation: The DeptNum reference in the CHANGE command is understood by the compiler to refer to the result set created by the second FIND command, causing unexpected behavior.

Technical Insight:

  • Implicit Result Set: The CHANGE command operates on the current set, which is determined by the last FIND command executed. This can lead to ambiguity if not managed carefully.

Example 3: Using External Result Sets

procedure test2()
   form open MyForm
   change MyForm from EmpSet
   form display
endprocedure
  • Explanation: EmpSet must be a set object in the application database or created by a set-processing command before compiling test2.

Technical Insight:

  • Set Object Requirement: Ensure that EmpSet exists and has the expected structure before using it in procedures. This can be achieved by creating a “dummy” set during compilation.

Example 4: Context-Sensitive Result Sets

procedure GetSet1 (a,b)
   if a=b
      find all Employees WorkIn Departments -> set1
   else
      find all Employees -> set1
   endif
   list set1
   bottom set1
   list set1
endprocedure
  • Explanation: Using set1 with different structures causes the compiled program to not work as expected. Ensure all FIND commands producing set1 have the same structure.

Technical Insight:

  • Consistent Structure: Maintain a consistent structure for result sets to avoid compilation issues. Use SET CHECKSETS ON to enforce continuity of structure.

Example 5: Consistent Result Set Structure

procedure GetSet1 (a,b)
   if a=b
      find all Employees WorkIn Departments -> set1
   else
      find all Employees WorkIn Departments where LastName = 'Smith' -> set1
   endif
   list set1
   bottom set1
   list set1
endprocedure
  • Explanation: Ensuring set1 has the same structure in all FIND commands avoids issues.

Technical Insight:

  • Compiler Consistency: The compiler can resolve references correctly when the structure of the result set remains consistent across different FIND commands.

Example 6: Resolving Field References

procedure test3 (a,b)
   if a=b
      find Employees -> EmpSet
   else
      find Departments -> DeptSet
   endif
   if a=b
      let a = LastName
   endif
endprocedure
  • Explanation: The compiler cannot resolve LastName in the LET command because it is not a field in the current set (DeptSet). It treats LastName as a character literal.

Technical Insight:

  • Field Resolution: The LET command can only refer to fields in the current set. Ensure the correct set is active when referencing fields.

Solutions to Example 6

Example 7: Save LastName in a Variable

procedure test3 (a, b, SavedLastName)
   if a=b
      find Employees -> EmpSet
      let SavedLastName = LastName
   else
      find Departments -> DeptSet
   endif
   if a=b
      let a = SavedLastName
   endif
endprocedure
  • Explanation: Save LastName in a variable immediately after finding EmpSet.

Technical Insight:

  • Variable Storage: Storing field values in variables ensures they are accessible even if the current set changes.

Example 8: Remove FIND to Another Procedure

procedure test3 (out a, b)
   if a=b
      find Employees -> EmpSet
   else
      findDeptSet % Call a program to find DeptSet.
   endif
   if a=b
      let a = LastName
   endif
endprocedure
  • Explanation: Remove the FIND command for DeptSet to another procedure.

Technical Insight:

  • Procedure Separation: Separating FIND commands into different procedures can prevent conflicts and ensure correct field references.

Example 9: Use SET CURRENTSET

procedure test3 (a, b)
   if a=b
      find Employees -> EmpSet
   else
      find Departments -> DeptSet
   endif
   if a=b
      set currentset EmpSet
      let a = LastName
   endif
endprocedure
  • Explanation: Use SET CURRENTSET to make EmpSet the current set inside the second IF statement.

Technical Insight:

  • Current Set Management: Explicitly setting the current set ensures the correct fields are referenced, avoiding ambiguity.

Conclusion: Understanding how result set references are compiled and managed in Zim is essential for developing robust applications. By following these examples and technical insights, developers can avoid common pitfalls and ensure their programs work as expected.

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *

pt_BRPortuguese