Greetings!

Consider the following code:

PUBLIC globalVariable as Boolean
PUBLIC global2

LOCAL result as Boolean

globalVariable = .f.

result = someFunction(@globalVariable)

MESSAGEBOX(TRANSFORM(globalVariable))
RELEASE ALL


FUNCTION someFunction
PARAMETERS someVariable
LOCAL functionResult
someVariable = .t.
RETURN .f.
ENDFUNC

The purpose of someFunction is to change the variable it receives. If the
calling program decides it wants the change to be kept, it can pass the
variable in by reference, as shown. But if the argument to be changed is a
global variable, then the global variable is not accessible inside
someFunction!

When I step through this code, I see that globalVariable and variable2 are
both defined before someFunction is called. When I step into someFunction,
the debugger suddenly says "Expression could not be evaluated" for
globalVariable! Why is this?

Thanks very much!

Rob

Re: Why do parameters hide global variables? by Gerben

Gerben
Tue Aug 10 08:51:32 CDT 2004

Hi Rob,

Parameters does not hide global, in your sample "someVariable" takes over
the "globalVariable", because your pass it by reference.

But why do you declare a global var and pass it by reference? That does not
make sence... Only when you use a local var, it makes sence if you pass it
by ref.
BTW using a lot of global vars are a bad habit anyway.

Hth,
Gerben Kessen


"Rob Richardson" <notreally@n2net.net> wrote in message
news:OZbTv9tfEHA.3556@TK2MSFTNGP12.phx.gbl...
> Greetings!
>
> Consider the following code:
>
> PUBLIC globalVariable as Boolean
> PUBLIC global2
>
> LOCAL result as Boolean
>
> globalVariable = .f.
>
> result = someFunction(@globalVariable)
>
> MESSAGEBOX(TRANSFORM(globalVariable))
> RELEASE ALL
>
>
> FUNCTION someFunction
> PARAMETERS someVariable
> LOCAL functionResult
> someVariable = .t.
> RETURN .f.
> ENDFUNC
>
> The purpose of someFunction is to change the variable it receives. If the
> calling program decides it wants the change to be kept, it can pass the
> variable in by reference, as shown. But if the argument to be changed is
a
> global variable, then the global variable is not accessible inside
> someFunction!
>
> When I step through this code, I see that globalVariable and variable2 are
> both defined before someFunction is called. When I step into
someFunction,
> the debugger suddenly says "Expression could not be evaluated" for
> globalVariable! Why is this?
>
> Thanks very much!
>
> Rob
>
>



Re: Why do parameters hide global variables? by Sietse

Sietse
Tue Aug 10 08:56:43 CDT 2004

Hi Rob,
In this case you're passing the global variable by reference to the
someFunction. The compiler notices that you're passing a variable by
reference and disables the other reference to the value. The other public
variable (global2) still can be accessed.
When you don't pass the value by reference the public var is also available.

HTH,
Sietse Wijnker


"Rob Richardson" <notreally@n2net.net> wrote in message
news:OZbTv9tfEHA.3556@TK2MSFTNGP12.phx.gbl...
> Greetings!
>
> Consider the following code:
>
> PUBLIC globalVariable as Boolean
> PUBLIC global2
>
> LOCAL result as Boolean
>
> globalVariable = .f.
>
> result = someFunction(@globalVariable)
>
> MESSAGEBOX(TRANSFORM(globalVariable))
> RELEASE ALL
>
>
> FUNCTION someFunction
> PARAMETERS someVariable
> LOCAL functionResult
> someVariable = .t.
> RETURN .f.
> ENDFUNC
>
> The purpose of someFunction is to change the variable it receives. If the
> calling program decides it wants the change to be kept, it can pass the
> variable in by reference, as shown. But if the argument to be changed is
a
> global variable, then the global variable is not accessible inside
> someFunction!
>
> When I step through this code, I see that globalVariable and variable2 are
> both defined before someFunction is called. When I step into
someFunction,
> the debugger suddenly says "Expression could not be evaluated" for
> globalVariable! Why is this?
>
> Thanks very much!
>
> Rob
>
>



Re: Why do parameters hide global variables? by Rob

Rob
Tue Aug 10 09:58:24 CDT 2004

Gerben,

Thanks for your response.

The purpose of the global variables is to control access to menus. The
variables are used in SKIP FOR clauses, so they have to be global, since
menus exist outside FoxPro's ownership paradigm. I originally was passing
them by reference since I wanted the function to work only on information
within its scope about rather than on variables outside its scope, but since
I needed to control three of them, I couldn't just use a return value from
the function.

Rob



Re: Why do parameters hide global variables? by Olaf

Olaf
Tue Aug 10 10:10:35 CDT 2004

> The purpose of the global variables is to control access to menus. The
> variables are used in SKIP FOR clauses, so they have to be global, since
> menus exist outside FoxPro's ownership paradigm.

you could still use function calls:
DEFINE BAR i OF yourpad PROMPT "menu entry" Skip for !MenuPermission(oApp.loggedinuser,i,"yourpad")

you can even do without skip:
DEFINE BAR i OF yourpad PROMPT IIF(MenuPermission(oApp.loggedinuser,i,"yourpad"),"","\ ")+"menu entry" KEY ...

You get the idea...

Bye, Olaf.



Re: Why do parameters hide global variables? by mspratt

mspratt
Tue Aug 10 10:36:18 CDT 2004

Hi Rob,

The variable is global.

Why do you bother to pass it?

Regards,

Mike

On Tue, 10 Aug 2004 09:42:27 -0400, "Rob Richardson"
<notreally@n2net.net> wrote:

>Greetings!
>
>Consider the following code:
>
>PUBLIC globalVariable as Boolean
>PUBLIC global2
>
>LOCAL result as Boolean
>
>globalVariable = .f.
>
>result = someFunction(@globalVariable)
>
>MESSAGEBOX(TRANSFORM(globalVariable))
>RELEASE ALL
>
>
>FUNCTION someFunction
> PARAMETERS someVariable
> LOCAL functionResult
> someVariable = .t.
> RETURN .f.
>ENDFUNC
>
>The purpose of someFunction is to change the variable it receives. If the
>calling program decides it wants the change to be kept, it can pass the
>variable in by reference, as shown. But if the argument to be changed is a
>global variable, then the global variable is not accessible inside
>someFunction!
>
>When I step through this code, I see that globalVariable and variable2 are
>both defined before someFunction is called. When I step into someFunction,
>the debugger suddenly says "Expression could not be evaluated" for
>globalVariable! Why is this?
>
>Thanks very much!
>
>Rob
>


Re: Why do parameters hide global variables? by Rob

Rob
Wed Aug 11 07:27:35 CDT 2004

Mike,

So that the function wouldn't care whether the variable was global or not,
or so I hoped. Because FoxPro is doing more thinking for me than I think it
should be doing, I have to assume that the function will always operate on
global variables. I don't like that assumption.

The behavior I was seeing was that whenever the global variable was hidden
by the system because the reference to it was created, the menu was being
automatically regenerated. I wonder if that was a consequence of SET
SYSMENU AUTOMATIC, which was part of the menu creation code that was
automatically generated by GENMENUX.

Rob