Get Rid of PUBLIC Variables

Well, in my programming techniques, I try very hard to avoid using PUBLIC variables. Then you ask me, how I can manage without using them ?? Avoiding Public variables has one common thing with avoiding smoking/drinking: you need the strong drive !! :)

This time, I tell you my way, I have one function called dPublic() - simulating the similar task with PUBLIC statement, as follows:

 
// dPublic() Function
 
#define PUB_NAME   1
#define PUB_VALUE  2
 
 
static _dPublic_ := 
 
function dPublic( cVarName, xNewValue )
  local xOldValue := nil
  local lVarExist := .F.
  local nCounter
 
  // If cVarName is a string, then proceed
  if valType( cVarName ) == "C"
    cVarName := upper( cVarName )
 
    // Searching in the Pseudo-Variable List
    for nCounter := 1 to len( _dPublic_ )
      if _dPublic_[ nCounter ][ PUB_NAME ] == cVarName
        // If found, then retrieve the current value into xOldValue
        xOldValue := _dPublic_[ nCounter ][ PUB_VALUE ]
        lVarExist := .T.
        // If there is a new value passed, then assign it
        if pCount() == 2
          _dPublic_[ nCounter ][ PUB_VALUE ] := xNewValue
        endif
      endif
    next
 
    // Check if it is a new variable
    if (! lVarExist) .and. pCount() == 2
      aAdd( _dPublic_,  )
    endif
 
  endif
return xOldValue


Download dPublic.zip now.