Lesson Details

Author: Trevor DeVore
Last Updated: 18 Apr 08:15

Tags

Revolution
0 comments for this lesson

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

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.

SetValueOfItInCaller Handler

command SetValueOfItInCaller pValue
    global gTempStorageForItVar

    ## Store value in global
    put pValue into gTempStorageForItVar

    ## Store current debugcontext (debugger uses this)
    put the debugcontext into theContext

    ## 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
    set the debugcontext to line -3 of the executioncontexts

    ## Make the global gTempStorageForItVar available within context of handler we
    ## want to set "it" in. Then set it to the global var
    debugdo "global gTempStorageForItVar;put gTempStorageForItVar into it"

    ## restore debugcontext
    set the debugcontext to theContext

    ## cleanup
    delete global gTempStorageForItVar
end SetValueOfItInCaller

Button Handlers

on mouseUp pMouseBtnNo
    if pMouseBtnNo is 1 then
        GetSomeDatabaseData
        put the result into theError

        if theError is empty then
            answer "Data returned is:" && it
        else
            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

Comments (0)

Add your comment

Are you human?