Abstract
This article describes a powerful technique that exploits my previously outlined innovation – Function Pointers in VB Script – to build a generic mechanism having the capacity of executing components repeatedly: the generic iterator.
Introduction
Executing processes repeatedly is a common theme in software development, one that is at the core of data processing in IT as well as other domains (RT, etc.). Automation developers also have the need to implement iteration code structures (ubiquitously called loops) in their scripts and functions, and this is not surprising because “… testing automation … is, indeed, a specific kind of software development …” (Implementing a GUI Layer with Classes, Bar-Tal, 2008), though many times I have encountered QA managers who count automation as a testing activity. For example, iterations are used for table data verification (GUI level), file processing, and for data-driving tests (i.e., executing the same test flow with different data values). Moreover, the ability to execute a previously coded procedure (or function) repeatedly is at the heart of automation; it is exactly what makes automation so appealing as a true alternative to putting a human tester to perform the tedious task of filling forms, pressing buttons and verifying results hundreds and even thousands of times. All the more, since such tasks are also repeated periodically – each firm having its own application version release policy (quarterly, semi-annually, and even weekly and bi-weekly schedule). Though basically such iteration structures are not difficult to implement, they are a major source of defects found in the code produced by developers. One central reason for this are faulty loop exiting conditions, which not always are well defined. Consequently, it would be of great importance to have a method of implementation that eliminates the need for coding the iteration structure again and again.
In previous articles we have seen that it is possible to exploit Object Oriented Design Patterns in order to improve the code used in our automated tests with regard to maintainability, readability, scalability, extensibility, reusability and testability. Moreover, it has been argued that using Function Pointers can help us to improve the performance of our code, and that this improvement correlates positively with the number of times a function is called (Bar-Tal, 2008). Combined with the fact that with other (OO) languages professionals define and implement iterator interfaces successfully and ubiquitously, all the above suggest that developing such an interface would most probably be worthwhile. As aforementioned, the method outlined here below is based on the basic techniques described in the article Function Pointers in VB Script (revised) (Bar-Tal, 2007, 2008). Hence that, before continuing, it is strongly recommended that the reader makes sure the ideas of function pointers and design patterns are well understood.
In this article I will propose a generic mechanism to handle iterations of executable code. I will attempt to show that using the method suggested here can have a beneficial impact on the efforts invested in development, testing, debugging and fixing defects, as it offers a service that can be used with ease. All it requires is the implementation of some very simple interfaces, as we will show in the next section.
Before delving into the actual implementation, let us define some terms. A generic iterator is a device capable of executing repeatedly a functionally independent executable component that performs a process upon a given object (which can be any data entity, such as a number or a mobile phone call data record). Since iterations are meaningful with variable inputs, it should be clear that the given process would act upon a different object in each iteration. Hence, the iterator would require at least two arguments: a collection of objects (operands) and a process (the operator). A process can be as simple as an arithmetic operation involving two numbers, but it can also be complicated as the calculation of a mobile customer bill, and more. Within the scope of the framework described hereby, the executable component that implements a process is built using the Command Wrapper design pattern. Functionally independent means that the specific blocks of code that we need to execute repeatedly are built without referencing any internals of the iterator device. In other words, the generic iterator does not care at all about the specific content included in the executable it activates, and the executable component does not need to take into account how the iterator that launches it is internally built. The only point in which both sides must be compatible is – as would be expected – with regard to their interfaces.
Method
In order to build the generic iteration mechanism we need to define the entities that will be involved in our implementation. Three entities must be defined:
-
The Iterator;
-
The required process or operation (packed using the Command Wrapper design pattern);
-
The object (class instance) upon which the process or operation should be applied.
Let us define the Iterator class as follows:
<span style="color: #606060"> 1:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 2:</span> <span style="color: #0000ff">Class</span> Iterator
<span style="color: #606060"> 3:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 4:</span> <span style="color: #008000">\'Class: Iterator</span>
<span style="color: #606060"> 5:</span> <span style="color: #008000">\'Encapsulates the Iterator Object</span>
<span style="color: #606060"> 6:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 7:</span> <span style="color: #008000">\'Remarks:</span>
<span style="color: #606060"> 8:</span> <span style="color: #008000">\' N/A</span>
<span style="color: #606060"> 9:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 10:</span> <span style="color: #008000">\'Owner:</span>
<span style="color: #606060"> 11:</span> <span style="color: #008000">\' Meir Bar-Tal, SOLMAR Knowledge Networks Ltd.</span>
<span style="color: #606060"> 12:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 13:</span> <span style="color: #008000">\'Date:</span>
<span style="color: #606060"> 14:</span> <span style="color: #008000">\' 17-Jan-2009</span>
<span style="color: #606060"> 15:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 16:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 17:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 18:</span> <span style="color: #0000ff">Public</span> <span style="color: #0000ff">Default</span> <span style="color: #0000ff">Function</span> Run(<span style="color: #0000ff">ByRef</span> objCollection, <span style="color: #0000ff">ByRef</span> ptrFunction, <span style="color: #0000ff">ByVal</span> strExitCondition)
<span style="color: #606060"> 19:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 20:</span> <span style="color: #008000">\'Function: Run</span>
<span style="color: #606060"> 21:</span> <span style="color: #008000">\'Performs n iterations of ptrFunction on the objCollection items where n=objCollection.count.</span>
<span style="color: #606060"> 22:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 23:</span> <span style="color: #008000">\'If the strExitCondition holds true, the next iteration is not performed and the function is exited.</span>
<span style="color: #606060"> 24:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 25:</span> <span style="color: #008000">\'Remarks:</span>
<span style="color: #606060"> 26:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 27:</span> <span style="color: #008000">\'Arguments:</span>
<span style="color: #606060"> 28:</span> <span style="color: #008000">\' ByRef objCollection - As Scripting.Dictionary</span>
<span style="color: #606060"> 29:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 30:</span> <span style="color: #008000">\' ByRef ptrFunction - As Function Pointer (to implement callback)</span>
<span style="color: #606060"> 31:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 32:</span> <span style="color: #008000">\' ByVal strCondition - String with condition to be evaluated.</span>
<span style="color: #606060"> 33:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 34:</span> <span style="color: #008000">\'Returns:</span>
<span style="color: #606060"> 35:</span> <span style="color: #008000">\' Scripting.Dictionary with the results of each iteration</span>
<span style="color: #606060"> 36:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 37:</span> <span style="color: #008000">\'Owner:</span>
<span style="color: #606060"> 38:</span> <span style="color: #008000">\' Meir Bar-Tal, SOLMAR Knowledge Networks</span>
<span style="color: #606060"> 39:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 40:</span> <span style="color: #008000">\'Date:</span>
<span style="color: #606060"> 41:</span> <span style="color: #008000">\' 17-Jan-2009</span>
<span style="color: #606060"> 42:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 43:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 44:</span> <span style="color: #0000ff">Dim</span> count, items, ix, str, dicResults
<span style="color: #606060"> 45:</span>
<span style="color: #606060"> 46:</span> <span style="color: #008000">\'Create a Dictionary to store the results for each iteration</span>
<span style="color: #606060"> 47:</span> <span style="color: #0000ff">Set</span> dicResults = CreateObject(<span style="color: #006080">"Scripting.Dictionary"</span>)
<span style="color: #606060"> 48:</span>
<span style="color: #606060"> 49:</span> <span style="color: #008000">\'Get the collection count</span>
<span style="color: #606060"> 50:</span> count = objCollection.Count
<span style="color: #606060"> 51:</span>
<span style="color: #606060"> 52:</span> <span style="color: #008000">\'Get the collection items</span>
<span style="color: #606060"> 53:</span> items = objCollection.Items
<span style="color: #606060"> 54:</span>
<span style="color: #606060"> 55:</span> ix = 0
<span style="color: #606060"> 56:</span> <span style="color: #0000ff">Do</span> <span style="color: #0000ff">While</span> ix < count
<span style="color: #606060"> 57:</span> <span style="color: #008000">\'Check if the exit condition holds true</span>
<span style="color: #606060"> 58:</span> <span style="color: #0000ff">If</span> Eval(strExitCondition) <span style="color: #0000ff">Then</span>
<span style="color: #606060"> 59:</span> dicResults(ix) = <span style="color: #006080">"Iteration "</span> & ix & <span style="color: #006080">" not performed."</span> & vbNewLine & <span style="color: #006080">"Condition \'"</span> & strExitCondition & <span style="color: #006080">"\' holds true. Exiting iterator."</span>
<span style="color: #606060"> 60:</span> <span style="color: #0000ff">Exit</span> <span style="color: #0000ff">Do</span>
<span style="color: #606060"> 61:</span> <span style="color: #0000ff">End</span> <span style="color: #0000ff">If</span>
<span style="color: #606060"> 62:</span>
<span style="color: #606060"> 63:</span> <span style="color: #008000">\'This statement performs the process/operation on the current item</span>
<span style="color: #606060"> 64:</span> dicResults(ix) = ptrFunction(items(ix))
<span style="color: #606060"> 65:</span>
<span style="color: #606060"> 66:</span> <span style="color: #008000">\'Increment the counter</span>
<span style="color: #606060"> 67:</span> ix = ix + 1
<span style="color: #606060"> 68:</span> <span style="color: #0000ff">Loop</span>
<span style="color: #606060"> 69:</span>
<span style="color: #606060"> 70:</span> <span style="color: #008000">\'Return Dictionary with the results</span>
<span style="color: #606060"> 71:</span> <span style="color: #0000ff">Set</span> Run = dicResults
<span style="color: #606060"> 72:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 73:</span> <span style="color: #0000ff">End</span> <span style="color: #0000ff">Function</span>
<span style="color: #606060"> 74:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 75:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 76:</span> <span style="color: #0000ff">End</span> <span style="color: #0000ff">Class</span>
<span style="color: #606060"> 77:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
Snippet 1. The Iterator Class
The reader certainly noticed the way an additional exit condition expression is passed to the Iterator (as a String) and how it is evaluated at the beginning of each iteration, before the actual operation is performed. This is also a powerful trick: for instance, we may pass to the Iterator Run method an exit condition that checks if an error occurred (“Err.Number <> 0″), after adding the On Error Resume Next and On Error Goto 0 statements at the appropriate places within the method.
Now, by implementing the Command Wrapper design pattern, let us define the arithmetic operations of addition, subtraction, multiplication, division, raising to a power and calculating the square root:
<span style="color: #606060"> 1:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 2:</span> <span style="color: #0000ff">Class</span> Add
<span style="color: #606060"> 3:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 4:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 5:</span> <span style="color: #0000ff">Public</span> <span style="color: #0000ff">Default</span> <span style="color: #0000ff">Function</span> Exec(<span style="color: #0000ff">ByVal</span> arrInt)
<span style="color: #606060"> 6:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 7:</span> <span style="color: #008000">\'Function: Exec</span>
<span style="color: #606060"> 8:</span> <span style="color: #008000">\'Executes the Command Wrapper\'s functionality</span>
<span style="color: #606060"> 9:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 10:</span> <span style="color: #008000">\'Remarks:</span>
<span style="color: #606060"> 11:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 12:</span> <span style="color: #008000">\'Arguments:</span>
<span style="color: #606060"> 13:</span> <span style="color: #008000">\' ByVal arrInt - As Array</span>
<span style="color: #606060"> 14:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 15:</span> <span style="color: #008000">\'Returns:</span>
<span style="color: #606060"> 16:</span> <span style="color: #008000">\' Result of operation</span>
<span style="color: #606060"> 17:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 18:</span> <span style="color: #008000">\'Owner:</span>
<span style="color: #606060"> 19:</span> <span style="color: #008000">\' Meir Bar-Tal, SOLMAR Knowledge Networks</span>
<span style="color: #606060"> 20:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 21:</span> <span style="color: #008000">\'Date:</span>
<span style="color: #606060"> 22:</span> <span style="color: #008000">\' 17-Jan-2009</span>
<span style="color: #606060"> 23:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 24:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 25:</span> Exec = arrInt(0)+arrInt(1)
<span style="color: #606060"> 26:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 27:</span> <span style="color: #0000ff">End</span> <span style="color: #0000ff">Function</span>
<span style="color: #606060"> 28:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 29:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 30:</span> <span style="color: #0000ff">End</span> <span style="color: #0000ff">Class</span>
<span style="color: #606060"> 31:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
Snippet 2. Addition
<span style="color: #606060"> 1:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 2:</span> <span style="color: #0000ff">Class</span> Subtract
<span style="color: #606060"> 3:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 4:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 5:</span> <span style="color: #0000ff">Public</span> <span style="color: #0000ff">Default</span> <span style="color: #0000ff">Function</span> Exec(<span style="color: #0000ff">ByVal</span> arrInt)
<span style="color: #606060"> 6:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 7:</span> <span style="color: #008000">\'Function: Exec</span>
<span style="color: #606060"> 8:</span> <span style="color: #008000">\'Executes the Command Wrapper\'s functionality</span>
<span style="color: #606060"> 9:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 10:</span> <span style="color: #008000">\'Remarks:</span>
<span style="color: #606060"> 11:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 12:</span> <span style="color: #008000">\'Arguments:</span>
<span style="color: #606060"> 13:</span> <span style="color: #008000">\' ByVal arrInt - As Array</span>
<span style="color: #606060"> 14:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 15:</span> <span style="color: #008000">\'Returns:</span>
<span style="color: #606060"> 16:</span> <span style="color: #008000">\' Result of operation</span>
<span style="color: #606060"> 17:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 18:</span> <span style="color: #008000">\'Owner:</span>
<span style="color: #606060"> 19:</span> <span style="color: #008000">\' Meir Bar-Tal, SOLMAR Knowledge Networks</span>
<span style="color: #606060"> 20:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 21:</span> <span style="color: #008000">\'Date:</span>
<span style="color: #606060"> 22:</span> <span style="color: #008000">\' 17-Jan-2009</span>
<span style="color: #606060"> 23:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 24:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 25:</span> Exec = arrInt(0)-arrInt(1)
<span style="color: #606060"> 26:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 27:</span> <span style="color: #0000ff">End</span> <span style="color: #0000ff">Function</span>
<span style="color: #606060"> 28:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 29:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 30:</span> <span style="color: #0000ff">End</span> <span style="color: #0000ff">Class</span>
<span style="color: #606060"> 31:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
Snippet 3. Subtraction
<span style="color: #606060"> 1:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 2:</span> <span style="color: #0000ff">Class</span> Multiply
<span style="color: #606060"> 3:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 4:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 5:</span> <span style="color: #0000ff">Public</span> <span style="color: #0000ff">Default</span> <span style="color: #0000ff">Function</span> Exec(<span style="color: #0000ff">ByVal</span> arrInt)
<span style="color: #606060"> 6:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 7:</span> <span style="color: #008000">\'Function: Exec</span>
<span style="color: #606060"> 8:</span> <span style="color: #008000">\'Executes the Command Wrapper\'s functionality</span>
<span style="color: #606060"> 9:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 10:</span> <span style="color: #008000">\'Remarks:</span>
<span style="color: #606060"> 11:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 12:</span> <span style="color: #008000">\'Arguments:</span>
<span style="color: #606060"> 13:</span> <span style="color: #008000">\' ByVal arrInt - As Array</span>
<span style="color: #606060"> 14:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 15:</span> <span style="color: #008000">\'Returns:</span>
<span style="color: #606060"> 16:</span> <span style="color: #008000">\' Result of operation</span>
<span style="color: #606060"> 17:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 18:</span> <span style="color: #008000">\'Owner:</span>
<span style="color: #606060"> 19:</span> <span style="color: #008000">\' Meir Bar-Tal, SOLMAR Knowledge Networks</span>
<span style="color: #606060"> 20:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 21:</span> <span style="color: #008000">\'Date:</span>
<span style="color: #606060"> 22:</span> <span style="color: #008000">\' 17-Jan-2009</span>
<span style="color: #606060"> 23:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 24:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 25:</span> Exec = arrInt(0)*arrInt(1)
<span style="color: #606060"> 26:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 27:</span> <span style="color: #0000ff">End</span> <span style="color: #0000ff">Function</span>
<span style="color: #606060"> 28:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 29:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 30:</span> <span style="color: #0000ff">End</span> <span style="color: #0000ff">Class</span>
<span style="color: #606060"> 31:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
Snippet 4. Multiplication
<span style="color: #606060"> 1:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 2:</span> <span style="color: #0000ff">Class</span> Divide
<span style="color: #606060"> 3:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 4:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 5:</span> <span style="color: #0000ff">Public</span> <span style="color: #0000ff">Default</span> <span style="color: #0000ff">Function</span> Exec(<span style="color: #0000ff">ByVal</span> arrInt)
<span style="color: #606060"> 6:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 7:</span> <span style="color: #008000">\'Function: Exec</span>
<span style="color: #606060"> 8:</span> <span style="color: #008000">\'Executes the Command Wrapper\'s functionality</span>
<span style="color: #606060"> 9:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 10:</span> <span style="color: #008000">\'Remarks:</span>
<span style="color: #606060"> 11:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 12:</span> <span style="color: #008000">\'Arguments:</span>
<span style="color: #606060"> 13:</span> <span style="color: #008000">\' ByVal arrInt - As Array</span>
<span style="color: #606060"> 14:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 15:</span> <span style="color: #008000">\'Returns:</span>
<span style="color: #606060"> 16:</span> <span style="color: #008000">\' Result of operation</span>
<span style="color: #606060"> 17:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 18:</span> <span style="color: #008000">\'Owner:</span>
<span style="color: #606060"> 19:</span> <span style="color: #008000">\' Meir Bar-Tal, SOLMAR Knowledge Networks</span>
<span style="color: #606060"> 20:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 21:</span> <span style="color: #008000">\'Date:</span>
<span style="color: #606060"> 22:</span> <span style="color: #008000">\' 17-Jan-2009</span>
<span style="color: #606060"> 23:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 24:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 25:</span> <span style="color: #0000ff">If</span> arrInt(1) <> 0 <span style="color: #0000ff">Then</span>
<span style="color: #606060"> 26:</span> Exec = arrInt(0)/arrInt(1)
<span style="color: #606060"> 27:</span> <span style="color: #0000ff">Else</span>
<span style="color: #606060"> 28:</span> Exec = <span style="color: #006080">"Error: Division by zero."</span>
<span style="color: #606060"> 29:</span> <span style="color: #0000ff">End</span> <span style="color: #0000ff">If</span>
<span style="color: #606060"> 30:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 31:</span> <span style="color: #0000ff">End</span> <span style="color: #0000ff">Function</span>
<span style="color: #606060"> 32:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 33:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 34:</span> <span style="color: #0000ff">End</span> <span style="color: #0000ff">Class</span>
<span style="color: #606060"> 35:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
Snippet 5. Division
<span style="color: #606060"> 1:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 2:</span> <span style="color: #0000ff">Class</span> Power
<span style="color: #606060"> 3:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 4:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 5:</span> <span style="color: #0000ff">Public</span> <span style="color: #0000ff">Default</span> <span style="color: #0000ff">Function</span> Exec(<span style="color: #0000ff">ByVal</span> arrInt)
<span style="color: #606060"> 6:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 7:</span> <span style="color: #008000">\'Function: Exec</span>
<span style="color: #606060"> 8:</span> <span style="color: #008000">\'Executes the Command Wrapper\'s functionality</span>
<span style="color: #606060"> 9:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 10:</span> <span style="color: #008000">\'Remarks:</span>
<span style="color: #606060"> 11:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 12:</span> <span style="color: #008000">\'Arguments:</span>
<span style="color: #606060"> 13:</span> <span style="color: #008000">\' ByVal arrInt - As Array</span>
<span style="color: #606060"> 14:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 15:</span> <span style="color: #008000">\'Returns:</span>
<span style="color: #606060"> 16:</span> <span style="color: #008000">\' Result of operation</span>
<span style="color: #606060"> 17:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 18:</span> <span style="color: #008000">\'Owner:</span>
<span style="color: #606060"> 19:</span> <span style="color: #008000">\' Meir Bar-Tal, SOLMAR Knowledge Networks</span>
<span style="color: #606060"> 20:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 21:</span> <span style="color: #008000">\'Date:</span>
<span style="color: #606060"> 22:</span> <span style="color: #008000">\' 17-Jan-2009</span>
<span style="color: #606060"> 23:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 24:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 25:</span> Exec = arrInt(0)^arrInt(1)
<span style="color: #606060"> 26:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 27:</span> <span style="color: #0000ff">End</span> <span style="color: #0000ff">Function</span>
<span style="color: #606060"> 28:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 29:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 30:</span> <span style="color: #0000ff">End</span> <span style="color: #0000ff">Class</span>
<span style="color: #606060"> 31:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
Snippet 6. Raise to a Power
<span style="color: #606060"> 1:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 2:</span> <span style="color: #0000ff">Class</span> SquareRoot
<span style="color: #606060"> 3:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 4:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 5:</span> <span style="color: #0000ff">Public</span> <span style="color: #0000ff">Default</span> <span style="color: #0000ff">Function</span> Exec(<span style="color: #0000ff">ByVal</span> varNumber)
<span style="color: #606060"> 6:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 7:</span> <span style="color: #008000">\'Function: Exec</span>
<span style="color: #606060"> 8:</span> <span style="color: #008000">\'Executes the Command Wrapper\'s functionality</span>
<span style="color: #606060"> 9:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 10:</span> <span style="color: #008000">\'Remarks:</span>
<span style="color: #606060"> 11:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 12:</span> <span style="color: #008000">\'Arguments:</span>
<span style="color: #606060"> 13:</span> <span style="color: #008000">\' ByVal varNumber - As Variant (Numeric))</span>
<span style="color: #606060"> 14:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 15:</span> <span style="color: #008000">\'Returns:</span>
<span style="color: #606060"> 16:</span> <span style="color: #008000">\' Result of operation</span>
<span style="color: #606060"> 17:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 18:</span> <span style="color: #008000">\'Owner:</span>
<span style="color: #606060"> 19:</span> <span style="color: #008000">\' Meir Bar-Tal, SOLMAR Knowledge Networks</span>
<span style="color: #606060"> 20:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 21:</span> <span style="color: #008000">\'Date:</span>
<span style="color: #606060"> 22:</span> <span style="color: #008000">\' 17-Jan-2009</span>
<span style="color: #606060"> 23:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 24:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 25:</span> <span style="color: #0000ff">If</span> varNumber <> 0 <span style="color: #0000ff">Then</span>
<span style="color: #606060"> 26:</span> Exec = Sqr(varNumber)
<span style="color: #606060"> 27:</span> <span style="color: #0000ff">Else</span>
<span style="color: #606060"> 28:</span> Exec = <span style="color: #006080">"Error: Square root for zero."</span>
<span style="color: #606060"> 29:</span> <span style="color: #0000ff">End</span> <span style="color: #0000ff">If</span>
<span style="color: #606060"> 30:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 31:</span> <span style="color: #0000ff">End</span> <span style="color: #0000ff">Function</span>
<span style="color: #606060"> 32:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 33:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 34:</span> <span style="color: #0000ff">End</span> <span style="color: #0000ff">Class</span>
<span style="color: #606060"> 35:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
Snippet 7. Square Root
Now let us define the objects on which these operations will be performed. As we are in the realm of arithmetic operations, obviously the objects must be numbers.
<span style="color: #606060"> 1:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 2:</span> <span style="color: #0000ff">Class</span> Number
<span style="color: #606060"> 3:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 4:</span> <span style="color: #0000ff">Private</span> m_intNumber
<span style="color: #606060"> 5:</span>
<span style="color: #606060"> 6:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 7:</span> <span style="color: #0000ff">Public</span> <span style="color: #0000ff">Default</span> <span style="color: #0000ff">Property</span> <span style="color: #0000ff">Get</span> Value()
<span style="color: #606060"> 8:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 9:</span> Value = m_intNumber
<span style="color: #606060"> 10:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 11:</span> <span style="color: #0000ff">End</span> <span style="color: #0000ff">Property</span>
<span style="color: #606060"> 12:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 13:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 14:</span> <span style="color: #0000ff">Public</span> <span style="color: #0000ff">Property</span> <span style="color: #0000ff">Let</span> Value(<span style="color: #0000ff">ByVal</span> intNumber)
<span style="color: #606060"> 15:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 16:</span> m_intNumber = intNumber
<span style="color: #606060"> 17:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 18:</span> <span style="color: #0000ff">End</span> <span style="color: #0000ff">Property</span>
<span style="color: #606060"> 19:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 20:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 21:</span> <span style="color: #0000ff">End</span> <span style="color: #0000ff">Class</span>
<span style="color: #606060"> 22:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
Snippet 8. The Number Class
We need a “constructor” function to create initialized instances of Number, as follows:
<span style="color: #606060"> 1:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 2:</span> <span style="color: #0000ff">Public</span> <span style="color: #0000ff">Function</span> CreateNumber(<span style="color: #0000ff">ByVal</span> intNumber)
<span style="color: #606060"> 3:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 4:</span> <span style="color: #008000">\'Function: CreateNumber</span>
<span style="color: #606060"> 5:</span> <span style="color: #008000">\'Creates a new instance of class Number</span>
<span style="color: #606060"> 6:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 7:</span> <span style="color: #008000">\'Remarks:</span>
<span style="color: #606060"> 8:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 9:</span> <span style="color: #008000">\'Arguments:</span>
<span style="color: #606060"> 10:</span> <span style="color: #008000">\' ByVal intNumber - As Integer</span>
<span style="color: #606060"> 11:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 12:</span> <span style="color: #008000">\'Returns:</span>
<span style="color: #606060"> 13:</span> <span style="color: #008000">\' Initialized Number object</span>
<span style="color: #606060"> 14:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 15:</span> <span style="color: #008000">\'Owner:</span>
<span style="color: #606060"> 16:</span> <span style="color: #008000">\' Meir Bar-Tal, SOLMAR Knowledge Networks</span>
<span style="color: #606060"> 17:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 18:</span> <span style="color: #008000">\'Date:</span>
<span style="color: #606060"> 19:</span> <span style="color: #008000">\' 17-Jan-2009</span>
<span style="color: #606060"> 20:</span> <span style="color: #008000">\'</span>
<span style="color: #606060"> 21:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 22:</span> <span style="color: #0000ff">Set</span> CreateNumber = <span style="color: #0000ff">New</span> Number
<span style="color: #606060"> 23:</span> CreateNumber.Value = intNumber
<span style="color: #606060"> 24:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
<span style="color: #606060"> 25:</span> <span style="color: #0000ff">End</span> <span style="color: #0000ff">Function</span>
<span style="color: #606060"> 26:</span> <span style="color: #008000">\'-------------------------------------------------------------------------------</span>
Snippet 9. The CreateNumber Function
And to end this section, let us illustrate how the entities defined above work in practice. Naturally, the example will deal with performing certain types of calculations to a list of numbers.
<span style="color: #606060"> 1:</span> <span style="color: #008000">\'Create a factor number to be used in the arithmetic operations</span>
<span style="color: #606060"> 2:</span> <span style="color: #0000ff">Set</span> factor = CreateNumber(5)
<span style="color: #606060"> 3:</span>
<span style="color: #606060"> 4:</span> <span style="color: #008000">\'Create a collection of number pairs using a dictionary</span>
<span style="color: #606060"> 5:</span> <span style="color: #0000ff">Set</span> dic = CreateObject(<span style="color: #006080">"Scripting.Dictionary"</span>)
<span style="color: #606060"> 6:</span> dic.Add <span style="color: #006080">"0"</span>, Array(CreateNumber(0), factor)
<span style="color: #606060"> 7:</span> dic.Add <span style="color: #006080">"1"</span>, Array(CreateNumber(1), factor)
<span style="color: #606060"> 8:</span> dic.Add <span style="color: #006080">"2"</span>, Array(CreateNumber(2), factor)
<span style="color: #606060"> 9:</span> dic.Add <span style="color: #006080">"3"</span>, Array(CreateNumber(3), factor)
<span style="color: #606060"> 10:</span> dic.Add <span style="color: #006080">"4"</span>, Array(CreateNumber(4), factor)
<span style="color: #606060"> 11:</span> dic.Add <span style="color: #006080">"5"</span>, Array(CreateNumber(5), factor)
<span style="color: #606060"> 12:</span> dic.Add <span style="color: #006080">"6"</span>, Array(CreateNumber(6), factor)
<span style="color: #606060"> 13:</span> dic.Add <span style="color: #006080">"7"</span>, Array(CreateNumber(7), factor)
<span style="color: #606060"> 14:</span> dic.Add <span style="color: #006080">"8"</span>, Array(CreateNumber(8), factor)
<span style="color: #606060"> 15:</span> dic.Add <span style="color: #006080">"9"</span>, Array(CreateNumber(9), factor)
<span style="color: #606060"> 16:</span>
<span style="color: #606060"> 17:</span> <span style="color: #008000">\'Create Iterator object</span>
<span style="color: #606060"> 18:</span> <span style="color: #0000ff">Set</span> objIterator = <span style="color: #0000ff">New</span> Iterator
<span style="color: #606060"> 19:</span>
<span style="color: #606060"> 20:</span> <span style="color: #008000">\'Set exit condition</span>
<span style="color: #606060"> 21:</span> strExitCondition = <span style="color: #006080">"GlobalStatus = 2"</span>
<span style="color: #606060"> 22:</span>
<span style="color: #606060"> 23:</span> <span style="color: #008000">\'Set GlobalStatus variable</span>
<span style="color: #606060"> 24:</span> GlobalStatus = 0
<span style="color: #606060"> 25:</span>
<span style="color: #606060"> 26:</span> <span style="color: #008000">\'Create Multiplication Operation</span>
<span style="color: #606060"> 27:</span> <span style="color: #0000ff">Set</span> ptrFunction = <span style="color: #0000ff">New</span> Multiply
<span style="color: #606060"> 28:</span> <span style="color: #008000">\'Print result</span>
<span style="color: #606060"> 29:</span> Msgbox Join(objIterator(dic, ptrFunction, strExitCondition).Items, vbNewLine), vbOKOnly+vbInformation, <span style="color: #006080">"Iterator Example - Multiplication by "</span> & factor
<span style="color: #606060"> 30:</span>
<span style="color: #606060"> 31:</span> <span style="color: #008000">\'Create Addition Operation</span>
<span style="color: #606060"> 32:</span> <span style="color: #0000ff">Set</span> ptrFunction = <span style="color: #0000ff">New</span> Add
<span style="color: #606060"> 33:</span> <span style="color: #008000">\'Print result</span>
<span style="color: #606060"> 34:</span> Msgbox Join(objIterator(dic, ptrFunction, strExitCondition).Items, vbNewLine), vbOKOnly+vbInformation, <span style="color: #006080">"Iterator Example - Addition of "</span> & factor
<span style="color: #606060"> 35:</span>
<span style="color: #606060"> 36:</span> <span style="color: #008000">\'Create Subtraction Operation</span>
<span style="color: #606060"> 37:</span> <span style="color: #0000ff">Set</span> ptrFunction = <span style="color: #0000ff">New</span> Subtract
<span style="color: #606060"> 38:</span> <span style="color: #008000">\'Print result</span>
<span style="color: #606060"> 39:</span> Msgbox Join(objIterator(dic, ptrFunction, strExitCondition).Items, vbNewLine), vbOKOnly+vbInformation, <span style="color: #006080">"Iterator Example - Subtraction of "</span> & factor
<span style="color: #606060"> 40:</span>
<span style="color: #606060"> 41:</span> <span style="color: #008000">\'Create Division Operation</span>
<span style="color: #606060"> 42:</span> <span style="color: #0000ff">Set</span> ptrFunction = <span style="color: #0000ff">New</span> Divide
<span style="color: #606060"> 43:</span> <span style="color: #008000">\'Print result</span>
<span style="color: #606060"> 44:</span> Msgbox Join(objIterator(dic, ptrFunction, strExitCondition).Items, vbNewLine), vbOKOnly+vbInformation, <span style="color: #006080">"Iterator Example - Division by "</span> & factor
<span style="color: #606060"> 45:</span>
<span style="color: #606060"> 46:</span> <span style="color: #008000">\'Create Power Operation</span>
<span style="color: #606060"> 47:</span> <span style="color: #0000ff">Set</span> ptrFunction = <span style="color: #0000ff">New</span> Power
<span style="color: #606060"> 48:</span> <span style="color: #008000">\'Print result</span>
<span style="color: #606060"> 49:</span> Msgbox Join(objIterator(dic, ptrFunction, strExitCondition).Items, vbNewLine), vbOKOnly+vbInformation, <span style="color: #006080">"Iterator Example - Raise number to the Power of "</span> & factor
<span style="color: #606060"> 50:</span>
<span style="color: #606060"> 51:</span> <span style="color: #008000">\'Create a collection of numbers using a dictionary</span>
<span style="color: #606060"> 52:</span> <span style="color: #0000ff">Set</span> dic = CreateObject(<span style="color: #006080">"Scripting.Dictionary"</span>)
<span style="color: #606060"> 53:</span> dic.Add <span style="color: #006080">"0"</span>, CreateNumber(100)
<span style="color: #606060"> 54:</span> dic.Add <span style="color: #006080">"1"</span>, CreateNumber(200)
<span style="color: #606060"> 55:</span> dic.Add <span style="color: #006080">"2"</span>, CreateNumber(300)
<span style="color: #606060"> 56:</span> dic.Add <span style="color: #006080">"3"</span>, CreateNumber(400)
<span style="color: #606060"> 57:</span> dic.Add <span style="color: #006080">"4"</span>, CreateNumber(400)
<span style="color: #606060"> 58:</span> dic.Add <span style="color: #006080">"5"</span>, CreateNumber(500)
<span style="color: #606060"> 59:</span> dic.Add <span style="color: #006080">"6"</span>, CreateNumber(600)
<span style="color: #606060"> 60:</span> dic.Add <span style="color: #006080">"7"</span>, CreateNumber(700)
<span style="color: #606060"> 61:</span> dic.Add <span style="color: #006080">"8"</span>, CreateNumber(800)
<span style="color: #606060"> 62:</span> dic.Add <span style="color: #006080">"9"</span>, CreateNumber(900)
<span style="color: #606060"> 63:</span>
<span style="color: #606060"> 64:</span> <span style="color: #008000">\'Create Square Root Operation</span>
<span style="color: #606060"> 65:</span> <span style="color: #0000ff">Set</span> ptrFunction = <span style="color: #0000ff">New</span> SquareRoot
<span style="color: #606060"> 66:</span>
<span style="color: #606060"> 67:</span> <span style="color: #008000">\'Print result</span>
<span style="color: #606060"> 68:</span> Msgbox Join(objIterator(dic, ptrFunction, strExitCondition).Items, vbNewLine), vbOKOnly+vbInformation, <span style="color: #006080">"Iterator Example - Square Root"</span>
<span style="color: #606060"> 69:</span>
<span style="color: #606060"> 70:</span> <span style="color: #008000">\'Dispose of Iterator</span>
<span style="color: #606060"> 71:</span> <span style="color: #0000ff">Set</span> objIterator = <span style="color: #0000ff">Nothing</span>
Snippet 10. Using the Iterator
Note: The code is full of comments to make it easier for the reader to understand. In case of need, please comment below the article and I will reply and give assistance.
Summary
This article has shown how to build and use a generic iterator written in VB Script. I pinpointed the motivation for building such a mechanism and explained its advantages for improving our development practices. I wish to encourage the readers to put hands on and try to implement and use the techniques described here and in other articles at www.AdvancedQTP.com, and to share with the community their experience in these matters. As aforesaid, please don’t hesitate to contact us or post your questions here below.



Recent Comments