Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Why setting var in CFFUNCTION causes error

Participant ,
Jan 26, 2009 Jan 26, 2009
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!!






TOPICS
Getting started
3.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 26, 2009 Jan 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>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 26, 2009 Jan 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>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 26, 2009 Jan 26, 2009
mega_L wrote:
> The problem I'm facing is I need to use more than 1 cfset var

That is perfectly allowable, you just need to put all of them in the
first block of the function. This is old school programing variable
declaration.

I.E.

> <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#">
<CFSET var ThisTitle = "I'm Nothing Important Yet">
<!--- or just "", if you prefer --->

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

Secondly, there is no necessity to copy the arguments scope into the
local variable scope. They are already local to the function and can
safely be used as #arguments.name1# and #arguments.NameID# throughout
the function.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 26, 2009 Jan 26, 2009
LATEST
> <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">

Bearing in mind the error message you mentioned, that VAR declarations need
to be at the top of the function... Where is this following VAR
declaration:

> <cfset var ThisTitle="President">

Is it at the top of the function? No. Does it need to be? Yes.

1) declare the variable (with the VAR keyword) @ the top of the function.
2) USE the variable anywhere you like after that.

--
Adam
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jan 26, 2009 Jan 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources