All righty then!
The "Getter/Setter" post generated a flurry of responses yesterday, and I've
taken a keen interest in it because like Gerry, I'm looking for some "best
practices".

Here's what I've learned so far:
1. 
<cfset Variables.Counter... in CF is like
DIM Counter as Variant      in VB, declaring a local variable.
<cfset This.Counter...      in CF is like 
PUBLIC Counter as Variant   in VB, declaring a global variable.

2.
Putting all your variables into a structure is a good idea for housekeeping
purposes, but to call your structure "instance" is intellectually dishonest.
I don't believe you are instantiating an object, and calling your structure
"instance" tells me that you'd like for it to be an object, but really
you're just mimicking OOP.
Admittedly, I don't understand OOP so I shouldn't be criticizing, but
calling a variable "Instance" confused me, even though I knew it was only an
example.

OK, now that I've offended some people, let me show you my humble offering
and let you tear it to shreds like Ray Camden would do if I were to submit a
"guess the number" page:

The file "mycomponent.cfc" contains:
<cfcomponent>
<cfset Variables.Counter = 0>

<cffunction name="getAge" access="public" returntype="string"
displayname="Get a person's age" hint="Plugh">
<cfargument name="BirthDate" type="date" required="yes">
<cfargument name="CurrentDate" type="date" default="#NOW()#">
<cfset Variables.Counter = Variables.Counter + 1>
<cfset Variables.Age = #DateDiff('yyyy',BirthDate,CurrentDate)#>
<cfreturn Variables.Age>
</cffunction>
</cfcomponent>

The file "myPage.cfm" contains:
<cfobject name="myObject" component="Components.mycomponent">
<cfset myBirthDate = CREATEODBCDATE('1/1/1900')>
<cfset myAge= myObject.getAge(myBirthDate)>
<cfoutput>#myAge#</cfoutput>



Variables.Counter isn't necessary, but I threw it in there to illustrate the
way you would DIM a variable at the top of a VB module to make it accessible
to the entire module, or in this case, all the functions within the
component.

How'd I do?


________________________________________
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Cody Caughlan
Sent: Wednesday, October 12, 2005 2:10 PM
To: [email protected]
Subject: RE: [CFCDev] Inside the CFC: use Getter/Setter or Instance Scope 

Always use getters/setters. This allows for encapsulation.
 
I use "variables.instance.propertyA", "variables.instance.propertyB", etc.
and then getters/setters for each, e.g.
 
<cffunction name"getPropertyA" access="public" returntype="WHATEVER">
    <cfreturn variables.instance.propertyA />
</cffunction>
 
And then you write a function similarly for the setter...
 
BTW, "Rooibos Generator" by Peter Farrell, accessible at:
 
 
 

________________________________________
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Gurevich, Gerry (NIH/NIEHS)
Sent: Wednesday, October 12, 2005 11:01 AM
To: [email protected]
Subject: [CFCDev] Inside the CFC: use Getter/Setter or Instance Scope 
If I’m inside a CFC and I’ve created an instance scope, how should I access
the properties of the object.

this.getBlah  or instance.Blah

Any thoughts?

-----------------------------------
Gerry Gurevich
Application Development
NIEHS ITSS Contractor
Lockheed Martin Information Technology
919-361-5444 ext 311




----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email to 
[email protected] with the words 'unsubscribe cfcdev' as the subject of the 
email.

CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting 
(www.cfxhosting.com).

CFCDev is supported by New Atlanta, makers of BlueDragon
http://www.newatlanta.com/products/bluedragon/index.cfm

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


Reply via email to