Returning Values from Handler

Hi

Trying to find out why my handler doesn’t add “Header-Blank” to the text string

set mySectionHeaderPref to "all blank"
set myBigOrderList to "a
b
c
d
"
--
--
addHeader(mySectionHeaderPref, myBigOrderList)
--
--
set myBigOrderList to myBigOrderList & "End"
--
--
on addHeader(mySectionHeaderPref, myBigOrderList)
	local mySectionHeaderPref, myBigOrderList
	--
	if mySectionHeaderPref is equal to "all blank" then
		set myBigOrderList to myBigOrderList & "Header-Blank" & linefeed
	end if
end addHeader

It a scope question and the answer is actually in your code.

Explicit

 local mySectionHeaderPref, myBigOrderList

– which is redundant by the way – declares myBigOrderList as (new) local variable in the scope of the handler which is different from the variable myBigOrderList in the implicit run handler.

You have a few options:

  1. Remove the second parameter and declare myBigOrderList in the handler as global.
on addHeader(sectionHeaderPref)
	global myBigOrderList
	
	if sectionHeaderPref is equal to "all blank" then
		set myBigOrderList to myBigOrderList & "Header-Blank" & linefeed
	end if
end addHeader
  1. Declare the outer myBigOrderList as property
property myBigOrderList : "a
b
c
d
"
...

on addHeader(sectionHeaderPref)
	if sectionHeaderPref is equal to "all blank" then
		set myBigOrderList to myBigOrderList & "Header-Blank" & linefeed
	end if
end addHeader
  1. Return the value and assign it back.
set myBigOrderList to addHeader(mySectionHeaderPref, myBigOrderList)

...


on addHeader(mySectionHeaderPref, myBigOrderList)
        -- in the handler myBigOrderList is a local variable, it's a different object as the variable in the run handler
	if mySectionHeaderPref is equal to "all blank" then
		set myBigOrderList to myBigOrderList & "Header-Blank" & linefeed
	end if
	return myBigOrderList 
end addHeader

thank you for your reply and explanation. :slight_smile: