Not that I'm not enjoying the rest of this thread, but did this particular poitn ever get answered officially? I'd really be curious to know if I should "scope" code like this.
Thanks, Nolan -----Original Message----- From: Jay Gibb [mailto:[EMAIL PROTECTED] Sent: Tuesday, February 10, 2004 4:11 PM To: [EMAIL PROTECTED] Subject: RE: [CFCDev] How should I define variable in a CFC? Are there any macromedia staff members on this list that can give us an authoritative answer? -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Barney Boisvert Sent: Tuesday, February 10, 2004 3:16 PM To: [EMAIL PROTECTED] Subject: RE: [CFCDev] How should I define variable in a CFC? It seems that Nathan found some contradicting evidence for the specifics of the CFQUERY return struct. Based on his research, it seem that the CFQUERY struct (and probably the other return structs from other tags) is immune to scoping issues, and the <cfset var cfquery = "" /> actually HIDES the return structure. So I guess those structures are outside the scope of race conditions because there can only be a single one alive at any time for a given request. So both your concerns are baseless, not to mention they don't actually make sense. 'Course, I didn't know they were nonsense until Nathan's post either. ; ) Cheers, barneyb > -----Original Message----- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of Jay Gibb > Sent: Tuesday, February 10, 2004 3:10 PM > To: [EMAIL PROTECTED] > Subject: RE: [CFCDev] How should I define variable in a CFC? > > Barney, > > Thanks for clearing that up... I have a couple follow-up questions.. > > 1) Does the same principle apply to all native CF tags that return > structures? (eg: cfhttp, cfftp, cfcatch, cffile, etc...) > > 2) Your answer seems to imply that a developer will need to > know if any > future function (within the CFC) will ever need to use the cfquery > structure. In other words, if I don't var scope my cfquery > variables now, > and a new function is added to the CFC down the road that > uses the cfquery > structure, will it run the risk of having problems? > > Thanks again, > - j. > > > > -----Original Message----- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] > Behalf Of Barney Boisvert > Sent: Tuesday, February 10, 2004 2:51 PM > To: [EMAIL PROTECTED] > Subject: RE: [CFCDev] How should I define variable in a CFC? > > > Yes, you are exactly correct. If you use the CFQUERY return > structure for > anything in any of your apps, then yes, you do need to > declare it. However, > as long as none of your code (CFC or client) has anything to > do with that > variable, then it doesn't really matter. > > Strictly speaking, you are perfectly safe never using the > 'var' keyword, as > long as you NEVER has multiple variables with the same name > (regardless of > scope, template, application, etc), and/or you NEVER > reference a variable > (to read or reset) after initially assigning it. > > Of course, neither of those conditions are realistic (can you > imaging naming > every CFLOOP index something different, rather than just > always using 'I'), > so we need the 'var' keyword. The second condition does > apply to my use of > the CFQUERY structure, however, so I don't need to scope it locally. > > Cheers, > barneyb > > > -----Original Message----- > > From: [EMAIL PROTECTED] > > [mailto:[EMAIL PROTECTED] On 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] > ---------------------------------------------------------- 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]
