Title: Persistence of unnamed-scope

Hi eric

 

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.

 

Pat

 

 

<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
To: [EMAIL PROTECTED]
Subject: [CFCDev] Persistence of unnamed-scope

 

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.

Reply via email to