Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check for an array's existance.

Usefull Functions & Procedures

How to check for an array's existance.

by  myearwood  Posted    (Edited  )
In my Foxpro Advisor article (subscription required): http://my.advisor.com/doc/17117

I argued that this line of code:
Code:
IF TYPE("ALEN(m.SomeVariable")
is not as readable, maintainable or extendable as:

Code:
IF X2IsArray(@m.SomeVariable)

Remember, copying code is not code reuse! ;)

The following code originated with me and was donated to the FoxPro community by Russ Swall President of VisionPace in memory of Drew Speedie.

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
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top