4 Replies - 2219 Views - Last Post: 30 September 2010 - 11:33 AM Rate Topic: -----

#1 xheartonfire43x   User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 454
  • Joined: 22-December 08

var Scoping and the variables scope

Posted 29 September 2010 - 11:21 AM

So this is probably a really simple question, but in a CFC how should I scope temp style variables in CFCs? I know the importance var scoping variables in CFCs, but sometimes I need like 5 temp variables at once. Sometimes I actually setup variables named temp1, temp2, temp3, temp4, temp5, while other times I store temp variables in a struct name temp. Is there another way to do this? Perhaps using the variables scope?
Is This A Good Question/Topic? 0
  • +

Replies To: var Scoping and the variables scope

#2 Craig328   User is offline

  • I make this look good
  • member icon

Reputation: 2042
  • View blog
  • Posts: 3,644
  • Joined: 13-January 08

Re: var Scoping and the variables scope

Posted 29 September 2010 - 12:43 PM

If the variable is only to be used inside the CFC then you can call them whatever you like with the exception of reserved words and such. Your description of using temp1, temp2 and such ought to be fine. Ad hoc scope names like that are referenceable only inside the CFC method itself. If you have one component method calling another, I'd try and make sure they don't use the same scope names just to be sure they don't overlap.

The variables scope is also available to use locally in the CFC method but again, it will only be available to that method and won't exist outside of it. Is there an issue you're running into with regard to variable naming? If you're looking for persistence outside of the variable scope I know the application scope is available and I believe the session scope is also accessible from inside the component method (although I'd check on the session one).
Was This Post Helpful? 0
  • +
  • -

#3 psykoprogrammer   User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 72
  • Joined: 09-October 06

Re: var Scoping and the variables scope

Posted 30 September 2010 - 07:05 AM

If you are setting up variables in a method inside of a CFC you must always VAR scope this. If you use the VARIABLES scope it will be available to *ALL* other methods in the CFC, as the VARIABLES scope is similar to Java's Private variables. ColdFusion 9 introduced the LOCAL scope for methods that is already VAR scoped for you, and is more convenient to use. For example:

<cfset local.temp1 = "test1" />
<cfset local.temp2 = "test2" />




View Postxheartonfire43x, on 29 September 2010 - 10:21 AM, said:

So this is probably a really simple question, but in a CFC how should I scope temp style variables in CFCs? I know the importance var scoping variables in CFCs, but sometimes I need like 5 temp variables at once. Sometimes I actually setup variables named temp1, temp2, temp3, temp4, temp5, while other times I store temp variables in a struct name temp. Is there another way to do this? Perhaps using the variables scope?

Was This Post Helpful? 0
  • +
  • -

#4 xheartonfire43x   User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 454
  • Joined: 22-December 08

Re: var Scoping and the variables scope

Posted 30 September 2010 - 08:08 AM

Okay, but in order to use something like the local scope in CF8 (or Railo and OpenBD probably) I would have to var scope a variable called local in my method first? I am really wondering what the best way to handle temp variables is, and setting a local struct to the var scope and using that sounds like the best.
Was This Post Helpful? 0
  • +
  • -

#5 Craig328   User is offline

  • I make this look good
  • member icon

Reputation: 2042
  • View blog
  • Posts: 3,644
  • Joined: 13-January 08

Re: var Scoping and the variables scope

Posted 30 September 2010 - 11:33 AM

The best way to do it...well...CF9 explicitly introduced the local scope for component methods. However, it's not new per se. Basically these two items are functionally the same:
<cfset var temp1="something">
<cfset local.temp1="something">

The first example is what you've always been able to do (pre-CF9) and the second example is what CF9 now allows you to do. In the past, you'd later refer to the stated variable as "temp1"...and, as we know, you run the risk (even though CF is supposed to evaluate the implied local variable scope first) you run whenever your reference an unscoped variable. The "new" local scope allows you to explicitly reference local.temp1 and then there's no question as to what you're working with.

My understanding is that the original implied local scope that was created with using the "var" keyword is backwards compatible because, as mentioned earlier, CF's scope order for evaluating an unscoped variable had them evaluating the local scope first anyway...but this change makes the way they do things more consistent.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1