Scope Conflicts


Yesterday I was helping someone with a problem, they kept receiving an “Cannot declare a local variable …. twice” error in one their DAO methods. It took us a couple of minutes to figure out how we were declaring the variable twice.  Come to find out var and cfargument are both in the local variable scope at least.
Example:

 <cffunction name="getName" acces="public" returntype="struct" output="false"> 
     <cfargument name="userID" type="Numeric" required="true" />
     <cfset var userID = "" /> <!--- Conflicts with arguments.userID --->
     <cfset var MyResults = StructNew() />
     <cfset MyResults.ResultCode = 200 />
     <cfset MyResults.ResultMessages = ArrayNew(1) />
     <!--- Your code here --->
     <cfreturn MyResults />
 </cffunction>

I will will be testing this on Railo and CF9 to see if they behave the same way….

Leave a Reply