I am getting a very strange error when I try to scope a private method within a ColdFusion component. I have this very simple ColdFusion component:
Launch code in new window » Download code as text file »
It does absolutely nothing. It has one private method named "PrivateMethod". It has two public methods; one, Debug(), that dumps out the variables scope, and another, CallMethod(), that invokes the private method in the VARIABLES scope using named arguments.
Now, if I call the Debug() method on this ColdFusion component I get:
| | | | ||
| | ![]() | | ||
| | | |
As you can clearly see, the method PrivateMethod() is clearly within the VARIABLES scope of this component. However, when I try to call CallMethod():
Launch code in new window » Download code as text file »
... I get this ColdFusion error:
Cannot invoke method PrivateMethod on an object of type coldfusion.runtime.VariableScope with named arguments. Use ordered arguments instead.
Now, I have two options to fix it here. I can either take out the names arguments to the PrivateMethod() function:
Launch code in new window » Download code as text file »
... and just use ordered arguments (values are assigned to arguments in the same order in which they were passed). Or, I can simply not use the a method scope when invoking the private method and let ColdFusion search for the appropriate method:
Launch code in new window » Download code as text file »
Both of the "solutions" will allow the code to execute fine. But, of course, this should not be the case. There is no reason that I can see that I should not be able to invoke a private method using its scope.
Any one have any ideas? Is this a bug? Am I just not seeing something?
Download Code Snippet ZIP File
Comments (5) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Cool. I knew about the variables.methodcall(namedArg="blah") because I tried it a while back. I just settled on taking out the reference to variables though, since I don't think it does anything for the readability of the program in that case (actually degrades it, imo). But I didn't know I could simply have used ordered arguments.
Strange stuff... keep us updated =)
Posted by Sam on Mar 12, 2007 at 8:48 AM
I head what you are saying on readability. My issues it that I like using the THIS scope when referencing public method within the same CFC. I learned to do this after I once had an ARGUMENTS key and a method name conflicting (it kept trying to invoke the argument "Commit" like it was a method (in the THIS scope)).
So anyway, if I use the THIS scope for public method invocation, which to me ups the readability (very clear what I am referring to), I figure I should use the VARIABLES scope for private methods in order to keep things very consistent.
Posted by Ben Nadel on Mar 12, 2007 at 8:59 AM
I agree with that - certainly in my mind if you can do this.publicmethod() you should be able to do this.privatemethod(). And adding the fact that you can use it with ordered arguments just strengthens the case.
Posted by Sam on Mar 12, 2007 at 9:12 AM
I had the same problem today invoking a function in the request scope. We typically copy our UDFs into the request scope so we can reference them from within CFCs.
The original code was:
<cfset var convRate = request.getConversionRate(foo = bar)>
The error was:
Cannot invoke method getConversionRate on an object of type coldfusion.runtime.RequestScope with named arguments.
Use ordered arguments instead.
The bizarre workaround was to create a copy of the function as a local variable and then invoke that copy instead. E.g.
<cfset var getConversionRate = request.getConversionRate>
<cfset var convRate = getConversionRate(foo = bar)>
This is pretty lame. Using ordered arguments wasn't an option because I have multiple optional arguments in my function.
Posted by Leon Miller-Out on Nov 29, 2007 at 5:20 PM
@Leon,
Yeah, I am not sure what the problem is with it. It must be some weird wiring issue behind the scenes that makes it very complicated. Cause to me, a scope is a scope is a scope.
Posted by Ben Nadel on Nov 29, 2007 at 5:36 PM