Search code examples
coldfusioncoldfusion-9

Access function arguments from another function


I am trying to create a custom debug tool and I need to use a component with two separate functions in it. The first function (startTimer) has some arguments such as startCount and the other one (endTimer) has endCount. What I am trying to accomplish is something like the following code:

<cffunction name="startTimer" access="public" returntype="void">
    <cfargument name="startActionTime" type="string" required="no">
</cffunction>

<cffunction name="endTimer" returntype="void" access="public">
    <cfargument name="endActionTime" type="string" required="no">

    <cfset finalTime = endActionTime - startTimer.startActionTime>

    <!---Some SQL will go here to record the data in a db table --->

</cffunction>

And this is how I am calling the function

<cfscript>
    location = CreateObject("component","timer");

    loc =location.startTimer(
        startActionTime = getTickCount()
    );


    end = location.endTimer(
        endActionTime = getTickCount()
    );


</cfscript>

I guess I am having scope issues because when I am trying to run the code I am getting an undefined error on startTimer.startActionTime. What is the correct way to do something like this?


Solution

  • You can use the variables scope like so:

    <cfcomponent>
    
      <cfset variables.startActionTime = 0>
    
      <cffunction name="startTimer" access="public" returntype="void">
        <cfargument name="startActionTime" type="numeric" required="no">
    
        <cfset variables.startActionTime = arguments.startActionTime>
      </cffunction>
    
      <cffunction name="endTimer" returntype="string" access="public">
        <cfargument name="endActionTime" type="numeric" required="no">
    
        <cfset finalTime = endActionTime - variables.startActionTime>
    
        <!---Some SQL will go here to record the data in a db table --->
        <Cfreturn finaltime>
    
      </cffunction>
    
    </cfcomponent>
    

    From the Adobe Docs: Variables scope variables created in a CFC are available only to the component and its functions, and not to the page that instantiates the component or calls its functions.