View comments | RSS feed
Contents > Developing ColdFusion MX Applications > Building and Using ColdFusion Components > Building ColdFusion components > Initializing instance data PreviousNext

Initializing instance data

Some components have instance data, data that persists as long as the component instance exists. For example, a shopping cart component might have instance data that includes the IDs and quantities of items the user puts in the shopping cart. Instance data is often shared by several methods that can create, delete, or modify the data. Components whose methods you invoke transiently, without first instantiating the component, do not typically have instance data.

You initialize instance data at the top of the component definition, before the method definitions. ColdFusion executes this code when it instantiates the component; for example, when a cfobject tag creates the component instance. Because this code executes only when the instance is created and it typically "constructs" properties of the component, instance data initialization code is sometimes called constructor code.

You can use any CFML tag or function in constructor code, and the code can perform any ColdFusion processing, such as querying a database or data validation and manipulation. If one component extends another, the parent component's constructor code executes before the child component's constructor code.

Note: ColdFusion does not require you to put the initialization code at the top of the component definition; however, it is good programming practice to do so.

The following example shows constructor code for a shopping cart CFC:

<cfcomponent>
   <!--- Initialize the array for the cart item IDs and quantities --->
   <cfset This.CartData = ArrayNew(2)>
   <!--- The following variable has the ID of the "Special Deal" product for 
         this session --->
   <cfset This.Special_ID = RandRange(1, 999)>

Note: For information on the This scope, see The This scope.


Contents > Developing ColdFusion MX Applications > Building and Using ColdFusion Components > Building ColdFusion components > Initializing instance data PreviousNext

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


TomChiv said on May 25, 2004 at 3:30 AM :
This page should include or link to information about when to use cfset this.varName and when to use var varName, with reference to which are public and which private, for instance.
ASandstrom said on May 25, 2004 at 9:27 AM :
In addition to reading the documentation that the link "The This scope" points to, it might be helpful to read the documentation about "The Variables scope" at:
http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/buildi47.htm
Generally, if you want to make variables public, use the This scope. If you want to make them private, use the Variables scope.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/buildi36.htm