4.3.6 Data utilities

Data utilities

String and array handling are quite comfortable and powerful in Visual FoxPro. However, there are a couple of details that could be handled a little better. The classes in this category attempt to fine-tune these capabilities.

Array Handler

Class

_arraylib

Base class

Custom

Class library

_utility.vcx

Parent class

_custom

Sample

...\Samples\Vfp98\Solution\Ffc\arrays.scx

Dependencies

_base.vcx

Visual FoxPro is rather flexible when it comes to array handling. However, I'd frequently like to have a couple of array functions that Visual FoxPro doesn't offer. Among them is scanning through an array column by column, deleting and inserting array items. The Array Handler object provides these functions.

The Array Handler should be instantiated as a generic object. Its methods can then be used like regular user-defined functions attached to an object. You simply call the methods and pass the array as well as some additional parameters to specify details about the array operation. The way these methods are called is very similar to calling array functions.

Let's look at the provided functions, the most used of which seems to be the "array scanning by column" feature. The AColScan() method implements this feature. The array and the search expression are passed as parameters like so:

oArray = NewObject("_arraylib","_utility.vcx")
DIMENSION aTest[3,3]
* In a real world scenario we would fill the array here
? oArray.AColScan(@aTest,"Search Value")

Note that the array must be passed by reference (using the @ sign.) The internal array handling functions do not require this, but the regular FoxPro language makes it impossible to pass arrays other than by reference. As you can see, the second parameter is the search string. By default, the method scans the first column of the array unless you specify the column number like so:

? oArray.AColScan(@aTest,"Search Value",2)

The return value is the absolute position of the found item. You can also specify that you want the row number to be returned. This can be done through the third parameter:

? oArray.AColScan(@aTest,"Search Value",2,.T.)

The next method I want to introduce is DelAItem(). This method is rather trivial; it deletes an entire row from an array, just like the ADel() function. The major difference is that the array also gets redimensioned after the row is deleted. The method requires two parameters the array (again passed by reference) and the row number you want to delete:

? oArray.DelAItem(@aTest, 2)

The Array Handler object also allows you to insert an item (similar to the AIns() function) by using the InsAItem() method. This method requires at least three parameters:

? oArray.InsAItem(@aTest, "Test", 2)

The first parameter is the array (again by reference). Parameter 2 is the value to be inserted, and parameter 3 is the number of the row after which the new item will be inserted. So in the example above, the new item will be inserted as row 3. As you can see, parameter 2 only allows passing a single value, which will be inserted into the array. In a multi-column array, this leaves us with default values for the rest of the columns. Those columns must either be populated afterwards, or we can specify that the passed value should be used for all columns. This can by done by passing a fourth parameter:

? oArray.InsAItem(@aTest, "Test", 2, .T.)

Overall, the Array Handler provides some nice functionality, but it's not like you couldn't live without it. Most important to me is the ability to scan single columns.

String Library

Class

_stringlib

Base class

Custom

Class library

_utility.vcx

Parent class

_custom

Sample

None

Dependencies

_base.vcx

The String Library doesn't really deserve its name. The class has only one tiny method TrimCRLF() which allows you to trim leading, trailing, or leading and trailing carriage returns and line feeds. Parameter 1 is the string that you want to trim, parameter 2 specifies whether leading characters should be trimmed, and parameter 3 does the same for trailing ones:

 

cString = Chr(13) + Chr(10) +;

Chr(13) + Chr(10) +;
"This is a test" +;
Chr(13) + Chr(10)
oStringLib = NewObject("_stringlib","_utility.vcx")
cString = oStringLib.TrimCRLF(cString,.T.,.T.)



Advanced Object Oriented Programming with Visual FoxPro 6. 0
Advanced Object Oriented Programming with Visual FoxPro 6.0
ISBN: 0965509389
EAN: 2147483647
Year: 1998
Pages: 113
Authors: Markus Egger

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net