[Closed] Getting Error : No outer local variable references permitted here?
fn testfn var = (
rollout test "Test"
(
button b1 "b1"
on b1 pressed do (var = 20)
)
createdialog test 200
)
testfn 10 --call to function
If I execute the code above I get following error, why?
-- Compile error: No outer local variable references permitted here: var
how can I change the value of var in event handler?
You cannot define a rollout inside a function. I don’t understand exactly what you’re trying to do, could you please elaborate on this?
- Enrico
try this
fn testfn var =
(
rollout test “Test”
(
local var
button b1 “b1”
on b1 pressed do (print var)
)
createdialog test 200
test.var = var
)
testfn 110 –call to function
as the compiler sad:
No outer local variable references permitted
That means that inside a rollout block you can’t use a local variable defined in a parent block, the local variable needs to be defined inside the rollout block or you can use a global one.
Try this:
var=10
fn testfn = (
rollout test "Test"
(
-- local var1=undefined
button b1 "b1"
on b1 pressed do (var = 20)
)
createdialog test 200
)
testfn()--call to function
cheers
In my program I must change the value of the incoming variable which is a function parameter, each and everytime I press button in my rollout.
There must be some alternate ways of accesing outer variable inside the rollout.
create the rollout with a checkbox, inside the function use a if that controls the rollout checkbox and set var to 20 if the checkbox is true
test=undefined
fn testfn var = (
if test.chkFlagVar.checked==true then var=20
print var
)
rollout test "Test"
(
checkbox chkFlagVar "var=20 :"
)
createdialog test 200
testfn 10
test.chkFlagVar.checked=true
testfn 10
It will not work because again you are hardcoding the value of VAR. what I want is when I check that checkbox or checkbutton the value of VAR will toggle between true and false.
like below— (it is not working code, but is just functionality i am looking for)
fn testfn VAR = (
rollout test "Test"
(
checkbutton b1 "b1"
on b1 changed state do ( if state VAR = true else VAR = false)
)
createdialog test 200
)
testfn true --call to function
but your function must wait until you press the button?
Your first example was with integer, your second with boolean…
It’s hard to help you if you don’t tell us what do you want
It doesnt matter what data type we are having with function (integer or boolean), the impotant thing here is to somehow make it work inside of the rollout.
the easiest solution is to use “global” variables (structures)…
(i prefer easiest ways and it’s how i usually delegate parameters to rollout)
The problem is that rollouts exist in their own scope, not the same scope as the variable. Similar to this…
rollout test "Test"
(
checkbutton b1 "b1"
on b1 changed state do (if state then VAR = true else VAR = false)
-- VAR is not the same VAR as in testfn. Two totally different scopes.
)
fn testfn VAR =
(
print VAR
)
testfn true --call to function
createdialog test 200
As you can see, the rollout is out of the scope of the function and VAR in the rollout is not the same VAR as the one in the testfn.
You can access and change the variable, however, if it exists elsewhere and you are accessing it and changing in that other location. This is what coren was showing. Here is another example, using a struct instead.
struct testS
(
VAR = true,
fn testfn =
(
rollout testR "Test"
(
checkbutton b1 "b1"
on b1 changed state do
(
test.VAR = state
format "test.VAR is now %
" test.VAR
)
)
createdialog testR 200
)
)
test = testS()
test.testfn() --call to function
This only works because we are calling on the variable as "test.VAR". This is leaving the scope of the rollout to access the variable in the scope of the struct.
As for a solution to the issue, I am not sure off the top of my head. It seems like it could be tricky, or even just not possible. There would have to be some intermediary functions I'm sure. Its hard to give a definitive answer without knowing the big picture of what you're trying to do. Its possible there might be an entirely different way of going about it which is much simpler. Maybe write another function that can figure out the location of the value that needs to be changed and then call that inside the event handler?