Passing variable to CFC?

I would like to be able to limit the number of news articles. For this particular page, I only want
to display 2.

I'm trying to pass a maxrows variable with the value of 2, but doesn't work. Am I going about this correctly or is there a better way? I'm trying to utilize one stored procedure.

<cfscript>      
  objNews = createObject("component","news");
  qNews = objNews.GetNews(maxrows = 2);  
</cfscript>

<cfcomponent>
<!--------------------------------------------------------
GetNews: Returns the news articles
--------------------------------------------------------->
<cffunction name="GetNews">
      <cfif IsDefined("maxrows")>
            <cfset maxrows = maxrows>
      <cfelse>
            <cfset maxrows = "-1">
      </cfif>      
      <cfstoredproc procedure="LC_GetNews" datasource="#request.datasource#" username="#request.username#" password="#request.password#">
            <cfprocresult name="qNews" maxrows="#maxrows#">
      </cfstoredproc>      
      <cfreturn qNews>
</cffunction>
</component>
LVL 8
JRockFLAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mkishlineCommented:
<cffunction name="GetNews">
  <cfargument name="maxrows" type="numeric" required="false" default="-1" />

and then the rest as is

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RCorfmanCommented:
The CFFunction, as defined doesn't take any arguments.  If it is located in the same .cfm as you are calling it from, then it can access maxrows directly, just assign it. If it ilocated in a .cfc, you need to create an argument in the cffunction using <CFargument>
mrichmonCommented:
Actually if you add the argument as mkishline showed, you don't have to leave the rest as is.  You can remove some code like this:

<cffunction name="GetNews">
     <cfargument name="maxrows" type="numeric" required="false" default="-1" />
     <cfstoredproc procedure="LC_GetNews" datasource="#request.datasource#" username="#request.username#" password="#request.password#">
          <cfprocresult name="qNews" maxrows="#maxrows#">
     </cfstoredproc>    
     <cfreturn qNews>
</cffunction>
</component>
JRockFLAuthor Commented:
Thank you mkishline!
Thank you RCorfman for a detailed explaination!
Thank you mrichmon for cleaning up the code!


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.