Scoping problem

Discussion in 'AutoCAD' started by perry, May 6, 2004.

  1. perry

    perry Guest

    I have a project which consists of several modules, one of which, the "main"
    module executes each time a drawing is opened and performs some tasks.
    My problem is I have several "global" vaiables which need to be accessed by
    different procedures in the various modules including the main one.
    To do this I just created a module named "globals" and put these variables
    in its declaration section, marking them as public.
    This works fine, the are accessable throughout the project. The problem is I
    need these variables to be re-initialized each time the main procedure runs.
    In
    other words, anytime a new drawing is opened I need these variables
    initialized for EACH drawing. That is not what Im getting now.
    For example, one of the variables is "titleblock", if I start Acad with a
    completed drawing, this variable will contain the name of the drawings
    titleblock.
    So far so good, but if I close the drawing and create a new (blank) drawing,
    this variable still contains the name of the titleblock from the previous
    drawing.
    If I declare them in the "main" module, with DIM, they will get
    re-initialized allright, but then they are not accessable by the other
    modules/procedures.
    Does anyone have a solution for my dilema????
    Thanks
     
    perry, May 6, 2004
    #1
  2. Simply reset the variable once the drawing has opened:

    for whatever
    open dwg
    globals.titleblock = new_titleblock
    do whatever
    close drawing
    next
     
    Mike Tuersley, May 6, 2004
    #2
  3. perry

    perry Guest

    Yeah, I guess I overlook the obvious. That is in essence what I did, and by
    resetting a key variable it prevented the others from
    getting bogus values.
    It did lead me to discover another problem though which is kinda the reverse
    of the previous.
    As I said this program runs each time a drawing is opened, and the
    aformention variables pick up new values. I found that if I have multiple
    drawins opened and then begin closing some, the variables do not revert to
    their previous values but retain info from the closed drawings! This led me
    to look for a "drawing closed" event I could hook into and tell my program
    to reinitialize when a drawing is closed, however no such event exists.
    There is a "begin close" event, but reinitializing during this doesnt work
    for me.
    In a nutshell what I want my program to do is spawn a new instance of itself
    with each drawing opened, maintain its variables for that particular drawing
    and when the drawing is closed release its OWN variables so that when I
    check the value of a variable its info is pertinant to the currently focused
    drawing. I was able to accomplish this via Autolisp with this same exact
    (language aside) program, but its not behaving as such in my VB version.
    I have obviously missed something again, there is a way for a routine to
    "exist" in different drawings with their own versions of variables isnt
    there (ala "namespace" in autolisp) ?
    Perry
     
    perry, May 7, 2004
    #3
  4. perry

    Tim J Guest

    From what you describe it sounds like creating a class would allow you to
    accomplish what you are after. Your class can have properties or variables
    for each value that you want control over for the instance of a drawing.
    You can also make any functions methods of the class to do any of the work.
    If you are using WithEvents when you Dim the AcadApplication, you can tell
    when a new drawing is opened, or begin close. The event triggered by a new
    drawing can create the instance of the class object, you can call the
    methods, then have begin close destroy the instance of the object.

    I do not have any code samples here, but the key is to encapsulate your code
    in a class module so that you can create specific instances of your object
    so there will be no mixing up of variables from one instance to another.
     
    Tim J, May 7, 2004
    #4
  5. perry

    perry Guest

    Well, yes true creating classes would allow me to instance objects with
    their own "protected" variables.
    Just not sure if I can do all I want inside a class. I'll give it a shot
    though.
    By "destroy the object" I assume you mean setting it to Nothing?
    Just seems like there should be another way of instancing a program with
    each drawing opened,
    that seems to be the default behavior in AutoLisp.
     
    perry, May 7, 2004
    #5
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.