I have a generic getter and setter that I use sometimes that looks like this:
<cffunction name="setProperty" access="public" returntype="void" output="False">
<cfargument name="PropertyName" type="string" required="true" />
<cfargument name="propertyValue" type="any" required="true" />
<cfargument name="PropertyType" type="string" required="false"
default="any">
<cfif arguments.propertyType NEQ "any" AND NOT
isValid(arguments.propertyType,propertyvalue)>
<cfthrow message="Invalid Type" detail="The value you provided
for
#arguments.propertyName# was not of type #arguments.propertyType#" />
<cfelse>
<cfset variables.instance.Property = arguments.Property />
</cfif>
</cffunction>
<cffunction name="getProperty" access="public" returntype="Any" output="false">
<cfargument name="PropertyName" type="string" required="true" />
<cfreturn variables.instance[arguments.propertyName] />
</cffunction>
That way you can set arbitraty values as properties, and get them back
out, as well as optional type checking.
I try to have getters and setters for everything, but if you had 100
different fields, this would be a good alternative.
On 3/16/07, Phillip Senn <[EMAIL PROTECTED]> wrote:
Here's a typical getter:
<cffunction name="getCustomerName" access="public" output="false"
returntype="string">
<cfreturn variables.strCustomerName />
</cffunction>
My impression is that if a table has 100 fields, then there are going to
be 100 getters.
Q: Is it possible to write a generic getter that uses perhaps an array
index or some other voodoo method instead of hard coding 100 getters?
<cffunction name="getter" access="public" output="false"
returntype="any">
<cfargument name="Index" required="true">
<cfreturn variables.Array[arguments.Index] />
</cffunction>
If the number 100 makes you uncomfortable, substitute the number 50, or
25. Whatever number you're comfortable with. I know some people balk
at a table having 100 fields.
You are subscribed to cfcdev. To unsubscribe, please follow the instructions at
http://www.cfczone.org/listserv.cfm
CFCDev is supported by:
Katapult Media, Inc.
We are cool code geeks looking for fun projects to rock!
www.katapultmedia.com
An archive of the CFCDev list is available at
www.mail-archive.com/[email protected]
--
Ryan Guill
A Deep Blue
[EMAIL PROTECTED]
www.ryanguill.com
(270) 217.2399
got google talk? Chat me at [EMAIL PROTECTED]
www.ryanguill.com/
A Deep Blue Blog: www.ryanguill.com/blog/
You are subscribed to cfcdev. To unsubscribe, please follow the instructions at
http://www.cfczone.org/listserv.cfm
CFCDev is supported by:
Katapult Media, Inc.
We are cool code geeks looking for fun projects to rock!
www.katapultmedia.com
An archive of the CFCDev list is available at
www.mail-archive.com/[email protected]