Interesting! I have also found that variables created in loops are a big problem. For example, suppose you want to create an array of structures. You write a loop that creates a structure, assigns some keys, and then appends the structure to the array. How do you declare the structure as local to that function? Well you can't without resorting to some drastic measures.

-Matt


On Feb 10, 2004, at 6:00 PM, Nathan Dintenfass wrote:


From the informal testing I have done, you don't need to worry about that.
CFQUERY will overwrite the cfquery variable every time a query is run. In
fact, from what I've seen, doing what you suggest with "var cfquery" doesn't
even work to keep the "cfquery" variable local to the method. For instance:


<cfcomponent>
<cffunction name="getEmployees">
        <cfset var get = "">
        <cfset var cfquery = "">
        <cfquery name="get" datasource="cfsnippets">
                select  *
                from    employees
        </cfquery>
        <cfreturn get>
</cffunction>
<cffunction name="getLastExecutionTime">
        <cfreturn cfquery.executionTime>
</cffunction>
</cfcomponent>


If you call getEmployees() and then getLastExecutionTime() you still get the
time of the query. To make things even stranger, if you add this line of
code inside getEmployees after the query:


<cfset writeOutput(cfquery.executionTime)>

it won't work unless you get rid of the var statement with cfquery.

Funky.





-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Behalf Of Jay Gibb
Sent: Tuesday, February 10, 2004 2:39 PM
To: [EMAIL PROTECTED]
Subject: RE: [CFCDev] How should I define variable in a CFC?


Hey Barney,


In your example below wouldn't you also need..

<cfset var cfquery = "">

So that the cfquery structure returned by the <cfquery> tag didn't get
dumped into the shared variables scope?

- j.

P.S. Pardon my re-iterating over this question, but it still confuses me.



-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Behalf Of Barney Boisvert
Sent: Tuesday, February 10, 2004 11:04 AM
To: [EMAIL PROTECTED]
Subject: RE: [CFCDev] How should I define variable in a CFC?


Yeah, you have to define everything first. And you have to set
them all to
a value as well. You can reasign later, though, and the variable name is
still protected. This comes in very handy with queries:


<cffunction name="getlist">
  <cfset var get = "" />
  <cfquery name="get" datasource="#variables.my.dsn#">
    SELECT *
    FROM myTable
    ORDER BY name
  </cfquery>
  <cfreturn get />
</cffunction>

You should do that for ALL queries, even if you don't need to use the name
afterwards (for insert, update and/or delete queries).


Cheers,
barneyb

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Schreck, Thomas (PPC)
Sent: Tuesday, February 10, 2004 10:57 AM
To: [EMAIL PROTECTED]
Subject: RE: [CFCDev] How should I define variable in a CFC?

That makes sense, but it seems somewhat limiting. What happens as you
process through the logic of your method and you need to set the value
of a variable? By declaring the variable with var at the top, is it
still protected further in your code? I assume so given your
answer (I
think I answered my own question). So, you must know all
variables you
want protected prior to getting into your method logic, correct?


Tom




-----Original Message----- From: Nathan Dintenfass [mailto:[EMAIL PROTECTED] Sent: Tuesday, February 10, 2004 12:45 PM To: [EMAIL PROTECTED] Subject: RE: [CFCDev] How should I define variable in a CFC?

As the error says, you can only use var at the beginning of a
method ;)

In other words, you can only declare your local variables immediately
after
CFARGUMENT and before any other code. Though, in 6.1 you can declare
them
at the top of the first CFSCRIPT block inside the function
body (a nice
improvement, as you had to use CFSET for all var declarations in 6.0).





-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Behalf Of Schreck, Thomas (PPC)
Sent: Tuesday, February 10, 2004 10:37 AM
To: [EMAIL PROTECTED]
Subject: RE: [CFCDev] How should I define variable in a CFC?


I've run into situations trying to use 'var' in front of a variable
name
and it produces an error. Something to the affect "var can only be
used
at the beginning of a method". What are the rules around using var?

Thanks - Tom Schreck


-----Original Message----- From: Barney Boisvert [mailto:[EMAIL PROTECTED] Sent: Tuesday, February 10, 2004 12:02 PM To: [EMAIL PROTECTED] Subject: RE: [CFCDev] How should I define variable in a CFC?

Using the 'var' keyword is only allowed inside
functions/methods. It
creates a local variable that only exists for that specific
invocation
of
the function.  Such variables are immune to race conditions, because
they
are bound to a specific invocation.  This is not specific
to CFCs, you
can
use it in any function definition (including CFSCRIPT functions), in
or
outside a CFC.

Using the 'variables' scope creates a private variable
accessible only
to
your CFC.  It exists as long as the instance of the CFC is around.
Such
variables are NOT immune to race conditions, because they are shared
across
the instance, and multiple clients may be calling methods
on a single
instance at the same time.

Using the 'this' scope is just like the 'variables' scope,
except that
the
variables are public, so the can be read or written by
client code, as
well
as methods of the CFC.

Cheers,
barneyb

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Troy Simpson
Sent: Tuesday, February 10, 2004 9:38 AM
To: [EMAIL PROTECTED]
Subject: [CFCDev] How should I define variable in a CFC?

All,

I have been away from coldfusion for about a year or so and
now need to
get back into in.  I am using ColdFusion MX for this project
that I am
working on.  I need some clarification if possible.

How should I define variables in a CFC and why?
What is the difference between the three?

<cfcomponent>
   <cfset var dsn = "DataSourceName">
   <cfset variables.dsn = "DataSourceName">
   <cfset this.dsn = "DataSourceName">
</cfcomponent>

Thanks,
Troy

--
Troy Simpson
   Applications Analyst/Programmer, OCPDBA, MCSE, SCSA
North Carolina State University Libraries
Campus Box 7111 | Raleigh | North Carolina
ph.919.515.3855 | fax.919.513.3330
E-mail: [EMAIL PROTECTED]

----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev'
in the message of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

An archive of the CFCDev list is available at
www.mail-archive.com/[EMAIL PROTECTED]


---------------------------------------------------------- You are subscribed to cfcdev. To unsubscribe, send an email to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev' in the message of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

An archive of the CFCDev list is available at
www.mail-archive.com/[EMAIL PROTECTED]
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev'
in the message of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

An archive of the CFCDev list is available at
www.mail-archive.com/[EMAIL PROTECTED]

----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev'
in the message of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

An archive of the CFCDev list is available at
www.mail-archive.com/[EMAIL PROTECTED]
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev'
in the message of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

An archive of the CFCDev list is available at
www.mail-archive.com/[EMAIL PROTECTED]


---------------------------------------------------------- You are subscribed to cfcdev. To unsubscribe, send an email to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev' in the message of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

An archive of the CFCDev list is available at
www.mail-archive.com/[EMAIL PROTECTED]


---------------------------------------------------------- You are subscribed to cfcdev. To unsubscribe, send an email to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev' in the message of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

An archive of the CFCDev list is available at
www.mail-archive.com/[EMAIL PROTECTED]

----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev'
in the message of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

An archive of the CFCDev list is available at www.mail-archive.com/[EMAIL PROTECTED]


----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev' in the message of the email.


CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

An archive of the CFCDev list is available at www.mail-archive.com/[EMAIL PROTECTED]

Reply via email to