Skip to Content
0
Former Member
Nov 11, 2006 at 10:45 AM

Mapping Arrays indexing in Crystal Reports formulas and indexes

137 Views

<span style="font-size: 10pt; font-family: &#39;Times New Roman&#39;"><p style="margin: 0in 0in 0pt" class="MsoNormal">I just took a Crystal Reports Designer class today and I presented the Array Mapping technique to the instructor. Since he was very interested I decided to post it here, so more people can benefit from it.</p> <p style="margin: 0in 0in 0pt" class="MsoNormal">This is an idea used a lot in C++. I designed a formula using two symmetric arrays. One to hold values to be used to test a condition, and a second one to hold values to be outputted when the condition is found. Since the index in the test values array for a given value to be tested matches the index of return values array of the correspondent output value, this methodology is called index mapping.</p> <p style="margin: 0in 0in 0pt" class="MsoNormal">Here follows the code:</p> <p style="margin: 0in 0in 0pt" class="MsoNormal">//testValues array store values to be tested in the formula</p><p style="margin: 0in 0in 0pt" class="MsoNormal">Local StringVar Array testValues :=<span> </span>MakeArray ( "S",<span> </span>"M",<span> </span><span> </span>"L",<span> </span>"XL");</p> <p style="margin: 0in 0in 0pt" class="MsoNormal">//returnValues array store values to be returned by the test formula</p><p style="margin: 0in 0in 0pt" class="MsoNormal">Local StringVar Array returnValues :=<span> </span>MakeArray ( "Small",<span> </span>"Medial",<span> </span>"Large",<span> </span>"Extra Large", </p><p style="margin: 0in 0in 0pt" class="MsoNormal"><span> </span>"App Type not Found");</p><p style="margin: 0in 0in 0pt" class="MsoNormal">Local NumberVar i := 1;</p><p style="margin: 0in 0in 0pt" class="MsoNormal">Local Numbervar returnIndex := -1;</p> <p style="margin: 0in 0in 0pt" class="MsoNormal">//Loop through the entire array for each values of the field</p><p style="margin: 0in 0in 0pt" class="MsoNormal">While<span> </span>i <= Ubound(testValues) AND returnIndex = -1 Do(</p><p style="margin: 0in 0in 0pt" class="MsoNormal">//Actual test of the values and return bellow</p><p style="margin: 0in 0in 0pt" class="MsoNormal"><span> </span>If ToText({CUSTOMER.SIZE}, 0) = testValues<i> Then( </p><p style="margin: 0in 0in 0pt" class="MsoNormal"><span> </span>returnIndex := i;);</p><p style="margin: 0in 0in 0pt" class="MsoNormal"><span> </span><span> </span>i := i + 1;</p><p style="margin: 0in 0in 0pt" class="MsoNormal">);</p> <p style="margin: 0in 0in 0pt" class="MsoNormal">//If any value found return it, else return last value in the return value array</p><p style="margin: 0in 0in 0pt" class="MsoNormal">If returnIndex = -1 Then</p><p style="margin: 0in 0in 0pt" class="MsoNormal"><span> </span>returnValues[Ubound(testValues) + 1]</p><p style="margin: 0in 0in 0pt" class="MsoNormal">Else</p><p style="margin: 0in 0in 0pt" class="MsoNormal"><span> </span>returnValues[returnIndex]</p> <p style="margin: 0in 0in 0pt" class="MsoNormal">Fell free to email me if you have questions and enjoy the code...</p></span>