Testing If a Member Exists

Date:    Wed, 1 Oct 1997 23:17:09 -0700
From:    Tom Collins <tom@newts.com>
Subject: Re: the exists of member "memberName"

>Naturally, if member "memberName" doesn't exist, then it can't have an
>"exists" property. Having said this, any one know any good strategies
>for finding out if a member "memberName" exists other than using the
>alertHook?  If so, I'd be happy to here about it.
I know other people have posted solutions, but I thought I'd share my memberExists and castExists handlers from my code library:

-- memberExists
-- params: param1, string, name of member to search for;
--         param2, optional, string or integer, castLib to search in
-- returns: TRUE if a member named mName exists in any (or the specified) castLib
on memberExists mName, cLib
  if not (stringP (mName)) then
    return FALSE   -- didn't pass a membername
  else if voidP (cLib) then   -- search all castLibs
    return (the number of member mName > 0)
  else
    if castLibExists (cLib) then
      return (the number of member mName of castLib cLib > 0)
    else return FALSE
  end if
end memberExists

-- castLibExists
-- params: param1, string or integer, castLib to search for
-- returns: number of castLib or 0 if castLib doesn't exist
on castLibExists cLib
  if integerP (cLib) then
    if (cLib > 0 and cLib <= the number of castLibs) then
      return cLib
    end if
  else if stringP (cLib) then
    repeat with i = 1 to the number of castLibs
      if the name of castLib i = cLib then return i
    end repeat
  end if
  return 0   -- cLib wasn't found (or wasn't a string/integer)
end castLibExists

It has the added benefit of checking to see if a member exists in a particular castLib.

 Tom Collins  | Innovative Computer Solutions | http://www.newts.com/
tom@newts.com +-------------------------------+-------------------------
(602)473-1240 | Custom software for Newton, Macintosh, Windows and Unix