Reputation: 4612
I've getting the following error when using the VARIABLES scope with a dynamic variable name:
Element wlc_period is undefined in a Java object of type class coldfusion.runtime.VariableScope.
When I try to run this code:
<cfparam name="wlc_period#y#" default="36">
<cfscript>
wlc_period = VARIABLES['wlc_period#y#'];
</cfscript>
But if I use evaluate()
, it works:
<cfscript>
wlc_period = evaluate('wlc_period#y#');
</cfscript>
At the time of execution, the y variable is an empty string, the code gets used elsewhere in a loop though, so this can sometimes be populated with an integer, 1 - 5.
The code should be trying to access wlc_period, which works if I just do a simple dump:
<cfparam name="wlc_period#y#" default="36">
<cfscript>
writedump(wlc_period);
</cfscript>
I'd rather use the VARIABLES scope instead of evaulate(), am I missing something here?
Upvotes: 0
Views: 284
Reputation: 2153
The following code works as expected for me.
<cfset y = "">
<cfparam name="wlc_period#y#" default="36">
<cfscript>
wlc_period = VARIABLES['wlc_period#y#'];
</cfscript>
<cfdump var="#wlc_period#">
If you have only that in a .cfm file, does that work for you?
I suspect that the issue you're experiencing is unrelated to the code that you posted.
Upvotes: 1
Reputation: 10682
I think you're misunderstanding what the VARIABLES
scope is. Or possibly what really scope variables are.
Scope variables are values stored in a specific set. For instance, the FORM
scope stores all values passed from a FORM post. The URL
scope stores all values passed in the query string of the URL.
The VARIABLES scope is for use in ColdFusion Components (CFCs). It is used to keep certain CFC properties reusable, but private. The VARIABLES scope is not simply an object that stores all variables.
Looking at your code, wlc_period#y
is simply a local variable that you're setting. I would suggest storing the value in a Local scope. I like to use "LOCAL" for local variables:
<cfparam name="LOCAL.wlc_period#y#" default="36">
<cfscript>
wlc_period = LOCAL['wlc_period#y#'];
</cfscript>
You probably should scope y
as well.
When you scope variables properly like this, you avoid running into problems where multiple scopes have the same variables.
Upvotes: 0