Alpha Video Training
Results 1 to 13 of 13

Thread: Question - variable scope between forms

  1. #1
    Member SNusa's Avatar
    Real Name
    Robert Tupper
    Join Date
    Dec 2007
    Location
    Northeast, USA
    Posts
    893

    Default Question - variable scope between forms

    If a form's object event runs a script (ie: a button on_click event), and that script opens a second form using:

    1.) script_play_local()
    Are pre-existing local variables available to this newly opened form?

    2.) script_play()
    Are pre-existing session variables available to this newly opened form?

    I'm thinking only global variables are visible to the second form.
    Robert T. ~ "I enjoy manipulating data... just not my data."
    It's all about the "framework." (I suppose an "a5-induced" hard drive crash is now in order?)
    RELOADED: My current posting activity here merely represents a "Momentary Lapse Of Reason."

  2. #2
    VAR csda1's Avatar
    Real Name
    Ira J Perlow
    Join Date
    Apr 2000
    Location
    Boston, Massachusetts, USA
    Posts
    3,530

    Default Re: Question - variable scope between forms

    Hi Robert,

    Quote Originally Posted by SNusa View Post
    If a form's object event runs a script (ie: a button on_click event), and that script opens a second form using:

    1.) script_play_local()
    Are pre-existing local variables available to this newly opened form?

    2.) script_play()
    Are pre-existing session variables available to this newly opened form?

    I'm thinking only global variables are visible to the second form.
    Script_play_local() allows access to the local variables (as well as the session (same as shared) and other variables that were available before that line.

    Script_play allows access to the session variables and other variables that were available before that line.

    BUT, when you start a form or other layout, in most (if not all) cases, you are starting a new session (synonomous with the word thread), which has it's own shared and local spaces. There are a few other things that implicitly or explicitly open new sessions (typically indicated by a new modeless window within Alpha Five), so when that code runs, the variable spaces don't follow into that window.
    Regards,

    Ira J. Perlow
    Computer Systems Design


    CSDA A5 Products
    New - Free CSDA DiagInfo - v1.39, 30 Apr 2013
    CSDA Barcode Functions

    CSDA Code Utility
    CSDA Screen Capture



  3. #3
    "Certified" Alphaholic
    Real Name
    Richard Coleman
    Join Date
    Oct 2000
    Location
    Franklin, TN
    Posts
    1,206

    Default Re: Question - variable scope between forms

    Wouldn't the use of global variables solve the problem?????
    D

  4. #4
    "Certified" Alphaholic G Gabriel's Avatar
    Real Name
    G. Gabriel
    Join Date
    Oct 2004
    Posts
    7,204

    Default Re: Question - variable scope between forms

    Quote Originally Posted by SNusa View Post
    I'm thinking only global variables are visible to the second form.
    If both forms are based on the same table, then shared variables will be visible to both. If based on 2 different tables, then you need global.

    The other 2 functions you referenced make the local variables dim'd in the original script (or function) visible to the calling script. It's a long winded way if your only goal is to make variables visible to both forms. If you use any of these 2 functions, then you have to save these scripts in the library (give them names) then call them with these functions in the second form!

    Just dim the variables shared or global. These function serve a purpose, but in this scenario, you don't need them.

  5. #5
    VAR csda1's Avatar
    Real Name
    Ira J Perlow
    Join Date
    Apr 2000
    Location
    Boston, Massachusetts, USA
    Posts
    3,530

    Default Re: Question - variable scope between forms

    Hi Dick,

    Quote Originally Posted by dik_coleman View Post
    Wouldn't the use of global variables solve the problem?????
    Robert wasn't expressing a problem, just what was accessible. But, yes, global variables would be accessible from any session. And if not dimensioned, it effectively bubbles up to looking at the global space for the variable name.



    Quote Originally Posted by G Gabriel View Post
    If both forms are based on the same table, then shared variables will be visible to both.
    This is incorrect. Create 2 forms with init events, 1 setting a dim'ed shared variable to a value, the 2nd displaying the value of a dim'ed value of the same name. Open up the 1st, which will set the value. Open up the second, and the value will not be there. Different sessions do not normally share the session variables

    However, proper use of WITH/END WITH can be used to make other variable spaces within the current code
    Regards,

    Ira J. Perlow
    Computer Systems Design


    CSDA A5 Products
    New - Free CSDA DiagInfo - v1.39, 30 Apr 2013
    CSDA Barcode Functions

    CSDA Code Utility
    CSDA Screen Capture



  6. #6
    "Certified" Alphaholic G Gabriel's Avatar
    Real Name
    G. Gabriel
    Join Date
    Oct 2004
    Posts
    7,204

    Default Re: Question - variable scope between forms

    Ira:
    That's not the scenario he described.
    Quote Originally Posted by SNusa View Post
    If a form's object event runs a script (ie: a button on_click event), and that script opens a second form
    Per help file:
    Session
    SHARED
    (e.g. dim SHARED NAME as C)
    A window is open. Even if you change the layout that is loaded in the window (by using the File > Open… command from within the Form or Browse window), the variable continues to exist.

  7. #7
    VAR csda1's Avatar
    Real Name
    Ira J Perlow
    Join Date
    Apr 2000
    Location
    Boston, Massachusetts, USA
    Posts
    3,530

    Default Re: Question - variable scope between forms

    Quote Originally Posted by G Gabriel View Post
    Per help file:
    Session
    SHARED
    (e.g. dim SHARED NAME as C)
    A window is open. Even if you change the layout that is loaded in the window (by using the File > Open… command from within the Form or Browse window), the variable continues to exist.
    That replaces the form in the current session with a different form, and since the session (thread) has not terminated, the shared (session) variables still exist.

    But it's not the same as opening a second form, which creates a new session and Alpha Five window, which is what Robert said.
    Regards,

    Ira J. Perlow
    Computer Systems Design


    CSDA A5 Products
    New - Free CSDA DiagInfo - v1.39, 30 Apr 2013
    CSDA Barcode Functions

    CSDA Code Utility
    CSDA Screen Capture



  8. #8
    "Certified" Alphaholic G Gabriel's Avatar
    Real Name
    G. Gabriel
    Join Date
    Oct 2004
    Posts
    7,204

    Default Re: Question - variable scope between forms

    Quote Originally Posted by csda1 View Post
    That replaces the form in the current session
    Isn't that what he is talking about?
    What would be the point of opening 2 different forms, based on the same table, simultaneously?

    Maybe I shouldn't ask. Recently, there was a thread about opening multiple forms based on the same table all at the same time. None of this make any sense to me, but I am sure someone will argue that it is necessary under certain circumstances.

  9. #9
    Member SNusa's Avatar
    Real Name
    Robert Tupper
    Join Date
    Dec 2007
    Location
    Northeast, USA
    Posts
    893

    Default Re: Question - variable scope between forms

    All the feedback and replies are extremely enlightening...
    I know the "simple answer" is "just use global vars."

    That being said, I guess a better way to word my question would be to present the folliwng scenario:

    If form1 button calls script in the code library. (passes all vars to script using script_play_local() function.).....
    Then this script opens Form2. (a different form)......

    Form2 can only see existing global variables, along with variables it (form2) creates. Concequently form2 can not "see" any of the variables that the script "sees" except for global ones.

    Bottom line:?????
    Forms can pass variables "down" to scripts
    Scripts can pass variables "into" other scripts called by the script.
    BUT generally speaking, scripts cannot pass variables up to forms (opened by the script.)

    (althouth there may be some way to do it, but not not by design - that's what global variables are for.)

    Right?

    ALSO...
    You presented two (2) interesting side questions...

    1.) What about opening a modal window? Same thing?

    2.) Exactly when are variables "destroyed?"
    Specifically, what happens to "passed" variables when form1 closes, but the script called by form1 is still running? Will this create errors within the script? Or do the variables passed from form1 remain intact for the life of the script? (Since the script "received them" prior to form1 actually closing?)

    These questions may seem "funky" but they provide much insight.

    I appreciate your responses.
    Robert T. ~ "I enjoy manipulating data... just not my data."
    It's all about the "framework." (I suppose an "a5-induced" hard drive crash is now in order?)
    RELOADED: My current posting activity here merely represents a "Momentary Lapse Of Reason."

  10. #10
    VAR csda1's Avatar
    Real Name
    Ira J Perlow
    Join Date
    Apr 2000
    Location
    Boston, Massachusetts, USA
    Posts
    3,530

    Default Re: Question - variable scope between forms

    Hi Robert,

    Quote Originally Posted by SNusa View Post
    Form2 can only see existing global variables, along with variables it (form2) creates. Concequently form2 can not "see" any of the variables that the script "sees" except for global ones.
    This is correct. You can pass a pointer to form 2 of the variable space of form 1 either through a global pointer, or if you identify the form 1's window, you can can get the shared and local pointers of form 1's spaces.

    Quote Originally Posted by SNusa View Post
    Bottom line:?????
    Forms can pass variables "down" to scripts
    Scripts can pass variables "into" other scripts called by the script.
    BUT generally speaking, scripts cannot pass variables up to forms (opened by the script.)
    This is correct as well. But you are better using functions, as they isolate it's variable spaces and allow better debugging. See my tips page.

    Quote Originally Posted by SNusa View Post
    (althouth there may be some way to do it, but not not by design - that's what global variables are for.)

    Right?
    Again, correct. If you can get a pointer to the window of the form, you can get everything that it contains and do everything just as if you were on that form. But remember, they are running independently. So if you were to write multiple values from form 2 to form 1, it's possible form1 might read partially written block of values as you are writing them. Thus you need to include a (semaphore or atomic) flag that says basically you are in the process of writing them followed by saying you are done. The real trouble here is Alpha 5 has no "test and set" instruction or something that can be used as such. (Only the locking of a record going into change mode accomplishes this). This means that just before you set the flag, your other window was reading the flag, both the read and write could proceed as if it was OK. It's a very small window, but possible. This makes inter-thread (read as session/window) communication potentially dangerous any time more than 1 item needs to be passed. Fortunately, it is never possible in Alpha to get a partially written single item. But if you extrapolate this, this same problem of inter-thread communication exists as well, even if you are using global variables to pass values. There are many that don't get this (and will never get this).

    The best method I can think of is to have a pointer to a bunch of empty variables, and then in 1 write, change the pointer to another pointer's value with all the correct values. At worst, the other thread will think that the values have not changed.

    Quote Originally Posted by SNusa View Post
    ALSO...
    You presented two (2) interesting side questions...

    1.) What about opening a modal window? Same thing?
    If it opens up another session/thread, it is the same.

    Quote Originally Posted by SNusa View Post
    2.) Exactly when are variables "destroyed?"
    Specifically, what happens to "passed" variables when form1 closes, but the script called by form1 is still running? Will this create errors within the script? Or do the variables passed from form1 remain intact for the life of the script? (Since the script "received them" prior to form1 actually closing?)
    When the window/session/thread closes, all of it's local and session variables go away. However passed pointers still point somewhere, so the variables under that pointer may possibly exist after that time, for some time, but I wouldn't count on it. Code containers that are executing at the close time contained within the window/session/thread will continue to their end, but anything needed by it contained within the window, and I say this with only 80% confidence) will be gone (with the exception of local variables to that code).

    Quote Originally Posted by SNusa View Post
    These questions may seem "funky" but they provide much insight.
    Actually, they are not "funky" at all. These are key programming concepts that few ask, and fewer understand. I suggest reading wikipedia and searching for atomic operations and semphores to understand multi-thread communications further.

    In most cases, Alpha programmers do not see this problem at all, because only 1 A5 window is active at a time, and the windows are all normally waiting for user input. But add a timing event on a form, and suddenly, you are potentially in a very dangerous world talking to other threads!
    Regards,

    Ira J. Perlow
    Computer Systems Design


    CSDA A5 Products
    New - Free CSDA DiagInfo - v1.39, 30 Apr 2013
    CSDA Barcode Functions

    CSDA Code Utility
    CSDA Screen Capture



  11. #11
    Volunteer Moderator Peter.Greulich's Avatar
    Real Name
    Peter Greulich
    Join Date
    Apr 2000
    Location
    Boston, MA
    Posts
    11,643

    Default Re: Question - variable scope between forms

    Robert,

    Just to add my 2-cents to this rather enlightened discussion, here are two sample code snippets showing how to get at variables in another session, one for local, the other for shared.

    Local:
    Code:
    DIM f as P
    DIM vLocalVars as P
    
    f= form.load("ProjContEntryDialog","Dialog","","center","center")
    vLocalVars = f.variables()        ''get the local vars
    vLocalVars.vCond1 = "Contact"
    f:cond1.refresh()
    Shared:
    Code:
    DIM f as P
    DIM s as P
    DIM sVars AS P
    DIM vDesc AS C
    
    f = form.load("AcctgBillMemos","dialog")
    f.show()
    
    ''Get the session variable in the loaded form namespace, so that we can fill in the invoice description
    
    s = f.SessionHandle()
    sVars = session_variables(s)
    vDesc = SVars.vInvoiceDesc
    
    f.close()
    
    ''other stuff here
    This is one way of avoiding using global variables.

  12. #12
    VAR csda1's Avatar
    Real Name
    Ira J Perlow
    Join Date
    Apr 2000
    Location
    Boston, Massachusetts, USA
    Posts
    3,530

    Default Re: Question - variable scope between forms

    Hi Peter,

    It was good showing the technique. I'll add 1 other recommendation for people in general. Very often they'll do a form open or similar, but never take the result from the operation, i.e.
    form.view("formname")
    This is bad, because you have no idea of what the window's name is (it could be an "alias" like formname0, if formname was already an object. Hence, it is always wise to save the pointer from the open, and then you can easily reference the form's session in the future, as in;
    formpointer=form.view("formname")
    By doing as you have, the form pointer can then be used with all the other needed code to operate in the session of the form.
    Regards,

    Ira J. Perlow
    Computer Systems Design


    CSDA A5 Products
    New - Free CSDA DiagInfo - v1.39, 30 Apr 2013
    CSDA Barcode Functions

    CSDA Code Utility
    CSDA Screen Capture



  13. #13
    Member SNusa's Avatar
    Real Name
    Robert Tupper
    Join Date
    Dec 2007
    Location
    Northeast, USA
    Posts
    893

    Default Re: Question - variable scope between forms

    CORRECTION NOTE: Peter G's Shared: code example (in post #11 above) incorrectly declared the local variable "s" as type P (pointer).
    It should be type N (numeric).

    Code:
    DIM f as P
    'DIM s as P ' - INCORRECT
    DIM s as N ' - CORRECT
    DIM sVars AS P
    DIM vDesc AS C
    
    f = form.load("AcctgBillMemos","dialog")
    f.show()
    
    ''Get the session variable in the loaded form namespace, so that we can fill in the invoice description
    
    s = f.SessionHandle()
    sVars = session_variables(s) ' - this function returns a numeric value
    vDesc = SVars.vInvoiceDesc
    
    f.close()  ' * don't close form too soon! (explained below)
    
    ''other stuff here
    ~This is a great technique/learning example! It's also important to realize: The sVars pointer is only a pointer to the original variables in the other layout..... Hence, when you close form "f" (regardless of whether or not it's modal/dialog) the values pointed to (by sVars) are destroyed.... * (So don't close form "f" before using/re-assigning these values!)

    PS: If you're having problems with Dialog (modal) forms not stopping code in the original calling form, (ie: not behaving "modal") here is the problem.
    Last edited by SNusa; 02-21-2012 at 03:19 PM.
    Robert T. ~ "I enjoy manipulating data... just not my data."
    It's all about the "framework." (I suppose an "a5-induced" hard drive crash is now in order?)
    RELOADED: My current posting activity here merely represents a "Momentary Lapse Of Reason."

Similar Threads

  1. Variable Scope Question
    By Scott_Walker in forum Alpha Five Version 9 - Desktop Applications
    Replies: 2
    Last Post: 11-08-2008, 12:59 AM
  2. A new variable scope
    By G Gabriel in forum Alpha Five Version 8
    Replies: 124
    Last Post: 12-17-2007, 12:28 PM
  3. Variable Scope and Calc fields in a sub-browse
    By dik_coleman in forum Alpha Five Version 8
    Replies: 5
    Last Post: 03-20-2007, 07:41 PM
  4. variable scope
    By dik_coleman in forum Alpha Five Version 5
    Replies: 6
    Last Post: 04-23-2004, 10:38 PM
  5. Action Scripting Variable Scope Bug
    By russ Boehle in forum Alpha Five Version 5
    Replies: 6
    Last Post: 08-28-2002, 04:43 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •