WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: mpiaser on July 10, 2013, 04:46:16 PM

Title: Global Variables
Post by: mpiaser on July 10, 2013, 04:46:16 PM
Is there a way to define a Global Variable in Winbatch.  I would like to be able to set a variable (Debug=Y or N) and be able to reference the variable in all future code include subroutines/functions/etc.
Title: Re: Global Variables
Post by: snowsnowsnow on July 10, 2013, 07:29:41 PM
The short answer is "No."

And believe me, I've spent a lot of time on this problem.  There is no actual solution.

However, there are lots of workarounds.  You can pretty much "name your poison".

Here are 3 methods that I know of, presented in my own personal order of preference:

1) The "XGLOB34i.DLL" extender.
2) Passing an array as a function parameter.
3) Using "pointers".  (Not available in all versions of WB)

I'm sure that others will provide the details on these various methods (and perhaps others...)

Title: Re: Global Variables
Post by: JTaylor on July 10, 2013, 07:55:29 PM
See if you have the PtrGlobalDefine(variable-name) and PtrGlobal (variable-name) functions.  They have been available for several years so you probably do.

Jim
Title: Re: Global Variables
Post by: mpiaser on July 10, 2013, 08:45:33 PM
The easiest solution to what I wanted was to change from DefineFunction to DefineSubroutine.
Title: Re: Global Variables
Post by: kdmoyers on July 11, 2013, 04:48:13 AM
Speaking for myself, the PtrGlobal functions are easy and effective.
-Kirby
Code (winbatch) Select
  dbg = @true
  ptrglobaldefine(dbg)

  #definefunction square(x)
      if *ptrglobal(dbg)
        message("x equals",x)
      endif
      return x * x
  #endfunction

  y = square(5)

  message(5,y)
Title: Re: Global Variables
Post by: Deana on July 11, 2013, 07:42:27 AM
Yes I would recommend using the PtrGlobal functions. See kdmoyers post for a good code sample.
Title: Re: Global Variables
Post by: nrr on July 11, 2013, 10:00:30 AM
+1 for the use of pointers.  Be careful changing DefineFunction to DefineSubroutine as you run the risk of inadvertenly changing variable values that you didn't intend to change.
Nick
Title: Re: Global Variables
Post by: snowsnowsnow on July 11, 2013, 10:02:27 AM
Quote from: mpiaser on July 10, 2013, 08:45:33 PM
The easiest solution to what I wanted was to change from DefineFunction to DefineSubroutine.

Yes, that's certainly the easiest "one-off" solution - and if it is just one function/subroutine and just one variable involved, then that's probably the best fix.  The question is: Does it scale?  (and: Does it work recursively?)

However, if it is just one variable, then I would like to suggest that you give the array idea a whirl.  That is, make your function like:

#DefineFunction myFun(myarg1,myarg2, A)
; Now use A[0] as if it was a global variable, which, in a sense, it is.
A[0] = "New value"
#EndFunction

; Mainline
A = Arrayize("value"," ") ; I think this is the right syntax; don't have manual handy ATM.
myFun(...,A)
; Now A[0] has a new value.
Title: Re: Global Variables
Post by: fhammer on January 31, 2016, 04:10:15 PM
LIMITED use of local environment variables has worked for me. You can create, and set them in a main script, and check them in lower level UDF functions. However, you can't set them externally, and then access them internally, or vice-versa. I also use parameter files to accomplish this.