{"id":502,"date":"2008-01-04T19:03:00","date_gmt":"2008-01-05T02:03:00","guid":{"rendered":"http:\/\/members.crystalreportsbook.com\/crystal-reports-xi\/8-7-arrays\/"},"modified":"2010-11-17T18:35:10","modified_gmt":"2010-11-18T01:35:10","slug":"8-7-arrays","status":"publish","type":"post","link":"http:\/\/www.crystalreportsonlinetraining.com\/training\/8-7-arrays\/","title":{"rendered":"8.06 Arrays"},"content":{"rendered":"<h2>Array Data Types<\/h2>\n<p>Arrays provide a means of storing a collection of data in a single variable and accessing each element of the array using an index. Unlike .NET arrays which are Base 0, an array in Basic syntax is Base 1. Thus, the first element is referenced using 1 as the index. The maximum size of an array is 1,000 elements.<\/p>\n<p>Basic syntax uses rounded parentheses to specify the array bounds.<\/p>\n<p><code_single>X(1) = 5<\/code_single><\/p>\n<p>Crystal syntax uses square parentheses to specify the array bounds.<\/p>\n<p><code_single>X[1] = 5<\/code_single><\/p>\n<p>When you declare an array, specifying the number of elements is optional. If you don&#8217;t specify the number of elements in the declaration, then before you use the array you have to either re-dimension it using the ReDim statement or assign an existing array to it.<\/p>\n<p><code>Dim X() As Number \u2018Declare a non-dimensioned array<\/code><br \/>\n<code>Dim Y(10) As Number  'Declare an array with 10 elements<\/code><\/p>\n<p>To declare an array with Crystal syntax, use the Array keyword after the data type.<\/p>\n<p><code>Local NumberVar Array X<\/code><br \/>\n<code>Local NumberVar Array Y[10]<\/code><\/p>\n<p>The ReDim statement is the same as in VB.NET. It will change the number of elements in the array and reset their values. If you want to keep the existing values intact, then also use the Preserve keyword.<\/p>\n<p><code>Redim var(number)\t\t\u2018Redminsion an array and reset all values<\/code><br \/>\n<code>Redim Preserve var(number)\t\u2018Redime an array and preserve existing values<\/code><\/p>\n<p>Assigning values to an array is done in different ways. If you know what the values of an array are during the development process, you can initialize the array with these values using the Array() function\u0002. Pass the Array() function all the elements as a comma delimited list and it returns an array that is fully populated with these elements. When using the Array() function you don&#8217;t have to specify the array size. The compiler figures that out for you.<\/p>\n<p>As expected, if you want to simply assign a value to an individual element in the array, then the index must be within the array&#8217;s bounds. But if you are assigning an entire array to another array variable and they are different sizes, you do not have to redimension the target array. The target array will be overwritten and it will have the size of the existing array.<\/p>\n<p><code>\u2018Demonstrate initializing an array and then overwriting it<\/code><br \/>\n<code>Dim MonthsInSeason() As String<\/code><br \/>\n<code>MonthsInSeason = Array(\"May\", \"June\")<\/code><br \/>\n<code>If LongWinter = True Then<\/code><br \/>\n<code>MonthsInSeason = Array(\"June\", \"July\", \"August\")<\/code><br \/>\n<code>End If<\/code><\/p>\n<p>If you don&#8217;t know the array values during the development process, you will probably assign the initial values to the array by looping through it. A common way of doing this is using a For Next loop where the range of the loop is the lower and upper bounds of the array. Another common method of assigning values to each element is to do so as the report is looping through its detail records. For each pass through the detail section you assign one of the fields to the array.<\/p>\n<p><code>\u2018Sample code within the detail section to track customer sales<\/code><br \/>\n<code>Dim RecordCounter As Number<\/code><br \/>\n<code>Dim SalesDetail() As Number<\/code><br \/>\n<code>RecordCounter = RecordCounter + 1<\/code><br \/>\n<code>SalesDetail(RecordCounter) = {Customer.Sales}<\/code><\/p>\n<p>Once the array is populated, you can test to see if a certain value already exists in the array by using the In operator. Using the In operator saves you the trouble of looping through the entire array searching for a particular value. If the value already exists, the In operator returns True.<\/p>\n<p><code>\u2018Sample code to fill an array with the unique zip codes for all customers being printed<\/code><br \/>\n<code>Dim RecordCounter As Number<\/code><br \/>\n<code>Dim ZipCodes(100) As Number<\/code><br \/>\n<code>If Not {Customer.ZipCode} In ZipCodes Then<\/code><br \/>\n<code>RecordCounter = RecordCounter + 1<\/code><br \/>\n<code>ZipCodes(RecordCounter) = {Customer.ZipCodes}<\/code><br \/>\n<code>End If<\/code><\/p>\n<p>Basic syntax has many predefined functions for summarizing the values in an array. These functions range from summing the total of all the values in the array to getting the maximum value in the array. Table 8-2 lists the array functions.<\/p>\n<p><strong>Table 8-2. Array Summary Functions<\/strong><\/p>\n<table border=\"1\">\n<tr>\n<th>Basic Function<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td>Average(array)<\/td>\n<td>Calculate the average of all numbers.<\/td>\n<\/tr>\n<tr>\n<td>Count(array)<\/td>\n<td>Count how many numbers there are.<\/td>\n<\/tr>\n<tr>\n<td>DistinctCount(array)<\/td>\n<td>Count how many numbers there are without including duplicates.<\/td>\n<\/tr>\n<tr>\n<td>Maximum(array)<\/td>\n<td>Return the maximum number.<\/td>\n<\/tr>\n<tr>\n<td>Minimum(array)<\/td>\n<td>Return the minimum number.<\/td>\n<\/tr>\n<tr>\n<td>PopulationStdDev(array)<\/td>\n<td>Return the Population Standard Deviation calculation.<\/td>\n<\/tr>\n<tr>\n<td>PopulationVariance(array)<\/td>\n<td>Return the PopulationVariance calculation.<\/td>\n<\/tr>\n<tr>\n<td>StdDev(array)<\/td>\n<td>Return the Standard Deviation calculation.<\/td>\n<\/tr>\n<tr>\n<td>Variance(array)<\/td>\n<td>Return the Variance calculation.<\/td>\n<\/tr>\n<\/table>\n<p>Although the functions are designed to only work with arrays, you can use them with table fields as well. This is done by creating a new array with the Array() function and passing it the fields to work with.<\/p>\n<p><code>\u2018Sample code for getting the maximum value of three fields<\/code><br \/>\n<code>Dim MaxSales1stQtr As Number<\/code><br \/>\n<code>MaxSales1stQtr = Max(Array({Sales.Jan}, {Sales.Feb}, {Sales.Mar}))<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Array Data Types Arrays provide a means of storing a collection of data in a single variable and accessing each element of the array using an index. Unlike .NET arrays which are Base 0, an array in Basic syntax is Base 1. Thus, the first element is referenced using 1 as the index. The maximum [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28,2],"tags":[],"class_list":["post-502","post","type-post","status-publish","format-standard","hentry","category-chapter-8-programming-with-basic-syntax","category-crystal-reportsnet-2003","entry"],"_links":{"self":[{"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/posts\/502","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/comments?post=502"}],"version-history":[{"count":1,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/posts\/502\/revisions"}],"predecessor-version":[{"id":1576,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/posts\/502\/revisions\/1576"}],"wp:attachment":[{"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/media?parent=502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/categories?post=502"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.crystalreportsonlinetraining.com\/training\/wp-json\/wp\/v2\/tags?post=502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}