Skip to main content
Inspiring
January 26, 2009
Question

Why setting var in CFFUNCTION causes error

  • January 26, 2009
  • 2 replies
  • 3598 views
As far as my knowledge goes, the BEST practise when setting local variable inside a CFFUNCTION is to use the word "var" in front of the variable name, for example: <CFSET var myVariable="whatever">
I'm doing it now and all I get is an error message saying:
Error invoking CFC name_of_cfc:local variable myVariable on line 60 must be grouped at the top of the function body!

I'm using CF8 and I need to use cfswitch within the cffunction, so when I do <cfcase value="SomeValue"><cfset var myVariable="TheValue"></cfcase> etc I got that error.
When I tested with just CFSET var MyVariable="TheVariable" with no cfswitch/cfcase I still get that error
when I took out the word "var" from cfset, I have no error, I put these variables directly after my cfarguments. Does anyone know why this is happening to me?

I thought if I don't use the "var" word for my variable set within cffunction there will be possibilities when later on setting up another variables outside this function and if the name happen to be the same, will override each other and causing disaster to my application.
Please help!!






This topic has been closed for replies.

2 replies

Participating Frequently
January 26, 2009
You say:
"I put these variables directly after my cfarguments."

But the error says:
"variable myVariable on line 60 must be grouped at the top of the function body!"

Please copy and paste the code, starting with <cffunction...> and down to <cfset var myVariable...>, exactly as it appears when you're getting this error.
Inspiring
January 26, 2009
mega_L wrote:
> As far as my knowledge goes, the BEST practise when setting local variable
> inside a CFFUNCTION is to use the word "var" in front of the variable name, for
> example: <CFSET var myVariable="whatever">

Yes, this is the best practice to make the variable local to the
function. But you only need to use the var keyword once when you define
the variable. And you must define the local variables with the var
keyword at the very beginning of the function body. The only content
allowed before this happens are the argument definitions, i.e. the
<cfargument ...> tags.

If you have a variable that you don't need to use, you just need to
define it with a default and|or dummy value in the beginning of the
function. Then you can set it's value appropriately later in the
function when it is required. This is often done with query tag return
variables.

I.E.
<cffunction name="myQryFunction"...>
<cfargument name="someArgument"...>

<cfset var localQry = "">
...
<cfquery name="localQry"...>
SQL
</cfquery>

<cfreturn localQery>
</cffunction>

Because of this necessity to define all local variables at the beginning
of the function, a lot of developers use a standard where they define a
local structure. They can then just append new keys and values to this
local structure throughout the function. Personally I don't see much
advantage to doing this, but to each his|her own.

I.E.
<cffunction name="myQryFunction"...>
<cfargument name="someArgument"...>

<cfset var local = structNew()>
...
<cfset local.something = "FooBar">

<cfreturn local.something>
</cffunction>
mega_LAuthor
Inspiring
January 26, 2009
The problem I'm facing is I need to use more than 1 cfset var
<CFFUNCTION name="editEdu">
<cfargument name="Name1">
<cfargument name="Name2"/>
<cfargument name="NameID"/>

<CFSET var Variable1="#Arguments.Name1.#">
<CFSET var Variable2="#Arguments.Name2#">
<CFSET var Variable3="#Arguments.NameID#">

<CFSWITCH expression="#Title#">
<CFCASE value="Pres">
<cfset var ThisTitle="President">
</CFCASE>
<CFCASE value="VP">
<cfset var ThisTitle="Vice President">
</CFCASE>
.........................etc
</CFSWITCH>

<cfquery name="team" datasource="cfdocexamples">
select columnames....from tablename
Where ID=#Variable3#
and Title= '#ThisTitle#'
</cfquery>


</CFFUNCTION>