Tutorials & ManualsHow-tosLessons MiscellaneousWrite A Command That Sets 'the result' And 'it'

Write A Command That Sets 'the result' And 'it'

Certain Revolution commands, such as 'answer', will set both it and the result:

* it is set to the data returned from the command

* the result contains status such as an error message or the button clicked on.

While it is not readily apparent how to do this when writing your own commands it is possible. Here is how.

SetValueOfItInCaller Command

command SetValueOfItInCaller pValue
    global gTempStorageForHandlerLocalVar
    ## Store value in global
    put pValue into gTempStorageForHandlerLocalVar
    ## Store current debugcontext (debugger uses this)
    put the debugcontext into theOrigContext
    ## the executionContexts is the list of handlers in the chain.
    ## This handler would be line -1.
    ## The handler that called this handler is -2.
    ## The handler that you want to set "it" in is -3
    put line -3 of the executioncontexts into theContext
    ## Account for behaviors in the context. Use repeat loop in case
    ## stack name has comma in it
    repeat for each item theItem in theContext
        add 1 to i
        if theItem is an integer then
            exit repeat
        end if
    end repeat
    delete item i + 1 to -1 of theContext
    set the debugcontext to theContext
    ## Make the global gTempStorageForHandlerLocalVar available within context of handler we 
    ## want to set "it" in. Then set it to the global var
    debugdo "global gTempStorageForHandlerLocalVar;put gTempStorageForHandlerLocalVar into it"
    ## restore debugcontext
    set the debugcontext to theOrigContext
    ## cleanup
    delete global gTempStorageForHandlerLocalVar
end SetValueOfItInCaller

The key to setting the value of it in the context of another handler is to use the executionContexts, debugContext and debugDo. By setting the debugContext we affect what handler the statements executed debugDo will affect. See the comments for more information.

Example 1

Let's look at how to use SetValueOfItInCaller in practice.

This is the script of a button. When the user clicks on the button GetSomeDatabaseData (1) is called. Database and XML calls are good candidates for setting both 'the result' and 'it' because you may encounter an error while trying to fetch the data. The error can be returned in 'the result'. The value fetched from the database or XML tree can be returned in it.

Since the result will contain any errors we assign the value to the theError variable and check whether or not that is empty (2). If it is empty then all is well and it will contain the data we want.

Running this example code will display the following dialog since the result is empty (3).

Example 2

This example returns an error string in the result (1) so the following dialog will be displayed.

Button Handlers

on mouseUp pMouseBtnNo
   if pMouseBtnNo is 1 then
      put the result into theError
      if theError is empty then
         answer "Data returned is:" && it
         answer "Error getting data:" && the result
      end if
   end if
end mouseUp

command GetSomeDatabaseData
   SetValueOfItInCaller "some data" ## 'it' in caller will contain "some data"
   return empty
   -- return "error connecting to database" ## 'the result' will contain the error message
end GetSomeDatabaseData

These are the handlers that go in the button.


Add your comment

E-Mail me when someone replies to this comment