Author Topic: usage of global variables  (Read 2860 times)

0 Members and 1 Guest are viewing this topic.

FengK

  • Guest
usage of global variables
« on: March 24, 2006, 04:47:14 AM »
Hello,

I used to think that it is not a good idea to use global variables in lisp codes and variables should be local to their corresponding commands.  Now I've completely changed my mind.  I use global variables a lot.  But to avoid making a big mess, all these global variables are named "glb::cmd_*", where cmd is the name of the command and "*" can be anything.  The primary reason for me to use global variables is to make user input a little easier.  If a user repeatly uses a command, his previous input can be used as the default value for later ones.  Or in some cases, user will not need to be asked for that input.  I have a command to clean these global variables when necessary.

(defun C:00 (/ strName) 
  (prompt "\nScanning for variables...")
  (princ)
  (foreach sym (atoms-family 0)
    (setq strName (strcase (vl-symbol-name sym)))
    (if   (wcmatch strName "GLB::*")
      (set sym nil)
    )
  )
  (prompt " Done.")
  (princ)
)

I'm thinking of triggering this when double-click on an empty spot occurs.

If a variable needs to be shared among open drawings, I use vl-bb-ref and vl-bb-set.

What's your thought?  Thank you.

Kelie





Jürg Menzi

  • Swamp Rat
  • Posts: 599
  • Oberegg, Switzerland
Re: usage of global variables
« Reply #1 on: March 24, 2006, 04:58:06 AM »
Kelie

Using global vars is a necessary thing in some code. My own rules:
- Default values for user prompts -> global vars
- Default values relating to drawing -> dictionary
- Default values relating to application -> registry (or ini file)
A computer's human touch is its unscrupulousness!
MENZI ENGINEERING GmbH
Current A2k16... A2k24 - Start R2.18

CADmium

  • Newt
  • Posts: 33
Re: usage of global variables
« Reply #2 on: March 24, 2006, 08:30:56 AM »
Kelie

Using global vars is a necessary thing in some code. My own rules:
- Default values for user prompts -> global vars
- Default values relating to drawing -> dictionary
- Default values relating to application -> registry (or ini file)

..and  Default values  for one ACAD-Session ..blackboard namespace -> vl-bb-ref / vl-bb-set functions, but i also prefer registry for this implementation
"Bei 99% aller Probleme ist die umfassende Beschreibung des Problems bereits mehr als die Hälfte der Lösung desselben."

GDF

  • Water Moccasin
  • Posts: 2081
Re: usage of global variables
« Reply #3 on: March 24, 2006, 10:02:32 AM »
Kelie

Using global vars is a necessary thing in some code. My own rules:
- Default values for user prompts -> global vars
- Default values relating to drawing -> dictionary
- Default values relating to application -> registry (or ini file)

I do something similar, but need to do more dictionaries. My globals or way overkill, but I'm still learning.

Gary
« Last Edit: March 24, 2006, 10:12:21 AM by Gary Fowler »
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

FengK

  • Guest
Re: usage of global variables
« Reply #4 on: March 25, 2006, 03:31:15 AM »

Using global vars is a necessary thing in some code. My own rules:
- Default values for user prompts -> global vars
- Default values relating to drawing -> dictionary
- Default values relating to application -> registry (or ini file)

Thanks Jürg.  I think I need to take a look at dictionary and registry now.

Jürg Menzi

  • Swamp Rat
  • Posts: 599
  • Oberegg, Switzerland
Re: usage of global variables
« Reply #5 on: March 25, 2006, 05:11:56 AM »
Welcome... :-)
You may check CADmium's recommendation also:
Quote
..and  Default values  for one ACAD-Session ..blackboard namespace -> vl-bb-ref / vl-bb-set functions, but i also prefer registry for this implementation
A computer's human touch is its unscrupulousness!
MENZI ENGINEERING GmbH
Current A2k16... A2k24 - Start R2.18