Wiki Home

X 2 ISARRAY


Namespace: WIN_COM_API
I had originally written Is Array, and contributed it to Drew Speedie for use in Visual Max Frame Professional. After Drew's death, I wrote an article in FoxPro Advisor Magazine about making code readable. You'd need a subscription to read the whole thing: http://my.advisor.com/doc/17117. Russ Swall of Vision Pace (makers of Visual Max Frame Professional) contributed the evolved version of Is Array - now called x2IsArray to the FoxPro community in Drew's memory.

Basically the point I was trying to make was this line of code:

IF TYPE("ALEN(m.SomeVariable)") = "N"


is much harder to read and extend and maintain than

If x2IsArray(@m.SomeVariable)


Removing the hardcoded first example to a single place, is like normalizing data. Don't forget copying code is not reuse. Reuse is a good thing. Here's the code:

*
*  X2IsArray.PRG
*  RETURNs a logical value indicating whether the
*  variable PASSED BY REFERENCE or the passed Object.Property
*  is an array.
*
*  Copyright (c) 2004-2005 Visionpace   All Rights Reserved
*                17501 East 40 Hwy., Suite 218
*                Independence, MO 64055
*                816-350-7900
*                http://www.visionpace.com
*                http://vmpdiscussion.visionpace.com
*  Author:  Drew Speedie
*           Special thanks to Mike Yearwood and Chris Bohling
*
*  USAGE
*  =====================================
*  IF X2IsArray(@m.SomeVariable)
*    ...
*  ENDIF
*  IF X2IsArray(SomeObject,"SomeProperty")
*    ...
*  ENDIF
*
*
*  lParameters
*   tuVariable (R) Memory variable to be checked,
*                    passed here BY REFERENCE
*                    -OR-
*                  Object whose tcProperty is to be
*                    checked
*   tcProperty (O) If tuVariable is passed as an object
*                    reference, this parameter is REQUIRED,
*                    and indicates the property of the
*                    tuVariable object that is checked for
*                    being an array
*                  If tuVariable is passed as a memory
*                    variable, DO NOT PASS THIS PARAMETER,
*                    or this routine will RETURN .F.
*
LPARAMETERS tuVariable, tcProperty

LOCAL llRetVal

DO CASE
  ******************************************************
  CASE PCOUNT() = 1 AND NOT VARTYPE(m.tuVariable) = "O"
  ******************************************************
    llRetVal = TYPE("ALEN(m.tuVariable)") = "N"
  ******************************************************
  CASE PCOUNT() = 1 AND TYPE("ALEN(m.tuVariable)") = "N"
  ******************************************************
    llRetVal = .t.
  ******************************************************
  CASE VARTYPE(m.tuVariable) = "O" ;
       AND VARTYPE(m.tcProperty) = "C" ;
       AND NOT EMPTY(m.tcProperty)
  ******************************************************
    llRetVal = TYPE("ALEN(m.tuVariable." + m.tcProperty + ")") = "N"
  ******************************************************
  OTHERWISE
  ******************************************************
    *
    *  you apparently haven't passed the parameters
    *  properly -- we could have RETURNed .NULL. here,
    *  but then every time you call X2IsArray(), you
    *  would have to check for .NULL, .T., and .F.
    *  rather than just .T. or .F., so it's up to you
    *  to pass the parameters correctly
    *    Roses are red
    *    Violets are blue
    *    To pass parms correctly
    *    Is all up to you
    *
    llRetVal = .f.
ENDCASE

RETURN m.llRetVal


Hi Mike, what's about using
   IF TYPE("m.SomeVariable",1) == "A"

No code to maintain, native VFP (since 9.0 or since 8.0?), best performance.
The only thing to do is: you should remember this language enhancement ;-)

The TYPE() function was extended in VFP 9.0 - and not just for arrays. If the second parameter is passed as 1 the following are returned:
A = Array
C = Collection
U = Not an Array

In addition two new return values were added
Q = Varbinary
W = Blob
Hi Andy

Think of x2isarray as a factory yielding the best way to detect an array in any version of VFP. -- Mike Yearwood

Not in any version, in VFP6 and up:) -- Cetin Basoz

The naming convension that the VMP framework uses suggests that it will work in FoxPro 2 and up. -- Peter Crabtree
It won't work in 2.6 because of the LPARAMETERS and VARTYPE() - however, because it's a separate function it can be made to work via compiler directives. Like Steve McConnell author of Code Complete said: The goal is to create routines with internal integrity (strong cohesion) and small, direct, visible, and flexible relations to other routines (loose coupling).

Contributors: Mike Yearwood, Andy Kramek
( Topic last updated: 2007.11.01 03:14:33 PM )