I know the cfproperty tag only creates metadata - I probably shouldn't even be using it since I would like to keep all of my instance properties private. There was a point raised in an earlier debate regarding whether the pseudo-constructor area would remain in subsequent versions of CF; I'm trying to build components that are as forward-compatible as possible. Since I'm not creating my internal properties with the "var" keyword, and I _always_ init() my objects, which creates the internal property variables, why is it that the variables are lost from function to function? I was under the impression that the variables in the unnamed scope, if not created with "var," persist inside the component.
Apparently, I'm wrong, so here's the meat of the question: how do I build a component to use private variables without coding their creation in the pseudo-constructor area?
Thanks,
ecd.
-----Original Message-----
From: Patrick Branley [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, August 13, 2003 7:01 PM
The problem you have is that you seem to be using cfproperty as a way in instantiating your variable in the unnamed scope. This is not however how cfproperty works. Cfproperty is acutally Meta-data for the component, not instance data of the component. This also means that it is not unique between instances of the same component.
What you need to do is use the implicit constructor code of cfc's to instantiate your variable. A good idea for variables in unamed scope is to use the underscore _ character so u set them apart from other variables.
Sample code below.
<cfcomponent output="no">
<cfscript>
_aProperty = ""; //I think you could also use cfparam
</cfscript>
<cffunction name="init" output="no" returntype="boolean">
<cfif ArrayLen(arguments) GT 0>
<cfif IsStruct(arguments[1])>
<cfset DSN = arguments[1] />
</cfif>
</cfif>
<cfreturn true />
</cffunction>
<cffunction name="getAProperty" returntype="string">
<cfreturn _aProperty />
</cffunction>
</cfcomponent>
-----Original Message-----
From: Davis, Eric [mailto:[EMAIL PROTECTED]]
Sent: Thursday, 14 August 2003 6:17 AM
Greetings, all.
As my organization has yet to install the 6.1 update, I am unable to take advantage of the variables-scope-like-use inside any CFCs. Once I can, my problem may be resolved; unfortunately, I can't wait that long to know.
The issue: I've got several components, each outlining their properties with the cfproperty tag, and each possessing an init() method wherein I pass a datasource collection and create the internal variables with any defaults.
Example:
<cfcomponent output="no">
<cfproperty name="aProperty" type="string" default="" />
<cffunction name="init" output="no" returntype="boolean">
<cfif ArrayLen(arguments) GT 0>
<cfif IsStruct(arguments[1])>
<cfset DSN = arguments[1] />
</cfif>
</cfif>
<cfset aProperty = "" />
<cfreturn true />
</cffunction>
</cfcomponent>
No problem, right? I invoke all of my CFCs with the CreateObject() function, then init() their variables, passing the DSN structure if the component is to access the database. I have yet to have a problem with this.
When I add another method, it doesn't seem to be able to see the value of the properties set by the init() function.
Example:
<cffunction name="getAProperty" returntype="string">
<cfreturn aProperty />
</cffunction>
This returns to my invoking template the following value:
[empty string]
Were aProperty to be an array, and the returntype of getAProperty() to be specified as "array" I would receive the error
"The value returned from function getAProperty() is not of type array."
Is the only way to maintain semi-private variables in the unnamed scope by creating them in the "constructor area" between property and function definitions?
Please help. You've taught me so much and I have so much more to learn.
Thanks,
ecd.
