Example: sharing the Variables scope
The following example shows how a CFC can change data in the calling page's Variables scope.
The callGreetMe.cfm page
<cfset Variables.MyName="Oswaldo">
<cfobject component="greetMe" name="myGreetings">
<cfoutput>
Before invoking the CFC Variables.Myname is #Variables.MyName#.<br>
Calling GreetMe CFC. It returns: #myGreetings.hello(VScope=Variables)#<br>
After invoking the CFC Variables.Myname is #Variables.MyName#.
</cfoutput>
The greetMe CFC
<cfcomponent>
<cffunction name="hello">
<cfargument name="Vscope" Required=true>
<cfset oldName=Arguments.VScope.MyName>
<cfset Arguments.VScope.MyName="Sandra">
<cfreturn "Hello " & oldName>
</cffunction>
</cfcomponent>
In this example, the callGreetMe.cfm does the following:
Sets the MyName variable in its Variables scope to "Oswaldo"
1
Displays the Variables.MyName value.
2
Calls the greetMe CFC and passes its Variables scope as a parameter.
3
Displays the value returned by the greetMe CFC.
4
Displays the Variables.MyName value.
5
The greetme.cfc component does the following:
Gets the calling page's Variables scope as the Vscope parameter.
1
Changes the value of the MyName variable in Arguments.Vscope to "Sandra"
2
Returns a string consisting of the original name from the Arguments.Vscope variable.
3
When you run the CFC, displays the following output, showing that the CFC changed the
MyName variable the calling page's Variables scope:
Before invoking the CFC Variables.Myname is Oswaldo.
Calling GreetMe CFC. It returns: Hello Oswaldo
After invoking the CFC Variables.Myname is Sandra.
The Arguments scope
The Arguments scope exists only in a method, and is not available outside the method. It contains
the variables passed into the method, including variables passed in the following ways:
•
As named attributes to the
•
In the
cfargumentcollection
•
In
cfinvokeargument
•
As attributes or parameters passed into the method when the method is invoked as a web
service, by Flash Remoting, as a direct URL, or by submitting a form.
tag
cfinvoke
attribute of the
tags
tag
cfinvoke
Building ColdFusion components
243
Need help?
Do you have a question about the COLDFUSION MX 61-DEVELOPING COLDFUSION MX and is the answer not in the manual?
Questions and answers