clear global variables...

Discussion in 'AutoCAD' started by aaron weissner, May 21, 2004.

  1. how do i clear all the global variables in a routine... i am calling
    several different routines from the one so i left all the variables as
    global instead of making them local... do i have to add something in there
    with all the variables like:

    (defun clear ()
    (setq var1 nil)
    (setq var2 nil)
    )

    or is there a better way...
     
    aaron weissner, May 21, 2004
    #1
  2. aaron weissner

    Mike Weaver Guest

    It's hard to say, without see the code, if this approach is "better" or not,
    but you could nest the defuns, then set declare the variables as local to
    the outer defun.

    Just a thought.

    Mike
     
    Mike Weaver, May 21, 2004
    #2
  3. aaron weissner

    ECCAD Guest

    Aaron,
    You could (setq var1 nil var2 nil var3 nil)..in the routine that
    calls the other routines. Do at end of main routine. If the var is already nil, no problem. Follow these lines with (gc).
    The alternative is - in each called routine, setq the vars nil that you (know) you won't be needing. Since all variables are by default 'global' unless 'localized' by a defun..it doesn't matter where you set them to nil.

    Bob
     
    ECCAD, May 21, 2004
    #3
  4. aaron weissner

    devitg Guest

    If you only use the variables inside your routine and di not want to use again after you exit the same , for me the best way is to declare it as local on the main defun

    (DEFUN C:DOTHIS ( / VAR1 VAR2 VAR3 VAR4 VAR5 )

    YOUR ROUTINE, ANY ONE YOU WANT
    KEEP ALL INSIDE THIS

    );END DEFUN C:DOTHIS

    SO AFTER IT ALL VARIABLES WILL BE CLEARED
     
    devitg, May 21, 2004
    #4
  5. aaron weissner

    Rakesh Rao Guest

    Hi Aaron,

    If it is unavoidable, you have to make variables as global. In that
    case, your method of setting the global variables to nil explicitly is
    fine. The other alternative is to declare variables as local within the
    function (by putting them after the /) or if that is not always
    possible, you could pass the variables as arguments to functions.

    What exactly you should be doing depends on your exact programming need
    but these are some broad guidelines.

    You may want to take a look at the Lisp library functions in our
    TechCenter web-site (see URL below). There are over 400+ examples of
    Lisp functions and they amply show how global variables, passed
    arguments and local variables are used in various situations as appropriate.

    Regards
    Rakesh

    --
    - Four Dimension Technologies [www.4d-technologies.com]
    - Get GeoTools, Work smarter, Free Download URL:
    www.4d-technologies.com/geotools
    - Free programming downloads @ TechCenter:
    www.4d-technologies.com/techcenter
     
    Rakesh Rao, May 22, 2004
    #5
  6. aaron weissner

    Rakesh Rao Guest

    Hi Aaron,

    If it is unavoidable, you have to make variables as global. In that
    case, your method of setting the global variables to nil explicitly is
    fine. The other alternative is to declare variables as local within the
    function (by putting them after the /) or if that is not always
    possible, you could pass the variables as arguments to functions.

    What exactly you should be doing depends on your exact programming need
    but these are some broad guidelines.

    You may want to take a look at the Lisp library functions in our
    TechCenter web-site (see URL below). There are over 400+ examples of
    Lisp functions and they amply show how global variables, passed
    arguments and local variables are used in various situations as appropriate.

    Regards
    Rakesh

    --
    - Four Dimension Technologies [www.4d-technologies.com]
    - Get GeoTools, Work smarter, Free Download URL:
    www.4d-technologies.com/geotools
    - Free programming downloads @ TechCenter:
    www.4d-technologies.com/techcenter
     
    Rakesh Rao, May 22, 2004
    #6
  7. aaron weissner

    Kerry Brown Guest

    Be careful what you wish for ..

    (defun nil-objectsbound (objectsbound /)
    ;;; kwb 2002.Jan.17
    (foreach varname objectsbound
    (if (= (type (setq tmp (vl-symbol-value varname))) 'vla-object)
    (if (not (vlax-object-released-p tmp))
    (vlax-release-object tmp)
    )
    )
    (set varname nil)
    )
    (princ)
    )

    (setq var1 100
    var2 12.5
    var3 'var2
    var4 (vla-get-layers
    (vla-get-activedocument
    (vlax-get-acad-object))
    )
    )
    .... < do-stuff >
    (nil-objectsbound (list 'var1 'Var2 'Var3 'Var4))

    regards.Kerry
    ;------------------------------------------------------------
    how do i clear all the global variables in a routine... i am calling
    several different routines from the one so i left all the variables as
    global instead of making them local... do i have to add something in
    there
    with all the variables like:

    (defun clear ()
    (setq var1 nil)
    (setq var2 nil)
    )

    or is there a better way...
     
    Kerry Brown, May 22, 2004
    #7
  8. aaron weissner

    Jamie Duncan Guest

    The other option to creating hordes of global variables is to create an
    association list as a global:

    eg
    Ms_global
    (("WINKIN" . 0)("BLINKIN" . "orange")("NOD" . "Jerseys"))

    <g>

    then to obtain a value of a global - use a local variable and:
    (setq local_winkin (cdr (assoc "WINKIN" ms_global)))

    for example

    and use subst to reset globals.

    then all of your vars are in a list, and it's easy to set them to nil, or
    add or change them with some simple startup functions...


    just a crazy idea...but it's nice to be able to see how they all change
    before and after running a routine.

    Jamie Duncan
     
    Jamie Duncan, May 22, 2004
    #8
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.