I'm trying to learn how to use CFCs properly in an OOP-like fashion
and have hit a problem which is stopping me from progressing.
I built a 'Question' class that's designed to model a multiple choice
question which can have a variable number of possible answers - these
are held in an array. The cfc code is below:
<cfcomponent displayname="Question" output="false">
<cfset variables.questionText = "">
<cfset variables.answers = arrayNew(1)>
<cfset variables.solutionIndex = 0>
<cffunction name="init" access="public" output="false"
returntype="Question" hint="I am the Constructor">
<cfreturn this>
</cffunction>
<cffunction name="setQuestion">
<cfargument name="questionText" type="string" required="true">
<cfset this.questionText = arguments.questionText>
</cffunction>
<cffunction name="getQuestion">
<cfreturn this.questionText>
</cffunction>
<cffunction name="setAnswer">
<cfargument name="answer">
<cfargument name="solution" default="false">
<cfset ArrayAppend(this.answers, arguments.answer)>
<cfif arguments.solution eq "true">
<cfset this.solutionIndex = arraylen(arguments.answer)>
</cfif>
</cffunction>
<cffunction name="getAnswers" returntype="array">
<cfif arraylen(this.answers) gt 0>
<cfreturn this.answers>
</cfif>
</cffunction>
</cfcomponent>
The problems arise when I try to set some answers using the
setAnswer() method using the following code:
<html>
<body>
<h1>Questions</h1>
<cfset q = CreateObject('component','cfc.Question')>
<cfset q.setQuestion('This is the question text')>
<cfset q.setAnswer('This is the first answer')>
<cfset q.setAnswer('The second answer',true)>
<cfset q.setAnswer('The penultimate answer')>
<cfset q.setAnswer('This is the last one')>
<cfdump var="#q#">
</body>
</html>
Running this, I get a CFML runtime error: this.answers doesn't exist.
Does anyone know why? Thanks
--
official tag/function reference: http://openbd.org/manual/
mailing list - http://groups.google.com/group/openbd?hl=en