Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Initializing Variable
Message
 
À
30/07/1998 01:54:39
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00122209
Message ID:
00122699
Vues:
24
>>>PRIVATE When initialized When creating routine ends The creating
> routine and any
> code called from
> it.<<
>
>Thanks, this is becoming much clearer now. Just for learning purposes, can you give me an example of how one would put PRIVATE to good use in a routine?

Rixon,

Private is not considered a good way of using variables as it promotes external coupling from the lower elvel routines to the higher one. However, there are times when you need it. For example;
PROCEDURE Level1
  * This variable is PRIVATE by default
  lcText = "ABC"
  DO Level2
  ? lcText
ENDPROC

PROCEDURE Level2
  * Here we are changing the value of the variable in Level1
  lcText = "DEF"
ENDPROC
A much better way of doing the above is;
PROCEDURE Level1
  LOCAL lcText
  lcText = "ABC"
  lcText = Level2(lcText)
  ? lcText
ENDPROC

PROCEDURE Level2
  LPARAMETERS pcVar
  IF pcVar = "ABC"
     pcVar = "DEF"
  ELSE
     pcVar = "DEF"
  ENDIF
  RETURN pcVar
ENDPROC
The second one is preferred because only Level 1 changes its variables, level 2 only receives a value from level1 and it returns a different value, whihc level1 puts in its variable. The only knowledge that evel2 has of level1 is the value passed, it does not need to know any variable names.

In the first example, if you changed the variable name in Level1 you would have to go and change Level2 as well. In the Level1 code the only clue you have of this requirement is that level2 is called.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform