[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Good CF function code / componant

Posted on 2003-11-25
14
Medium Priority
?
270 Views
Last Modified: 2013-12-24
i want this to be a function and therefore reusable. I dont mind using a componant:

<cfset myRand1 =RandRange(1,6)>
<cfset myRand2 =RandRange(1,6)>
<cfloop condition="#myRand1# eq #myRand2#">
<cfset myRand2 = RandRange(1,6)>
</cfloop>

it needs to return 2 random numbers between 1 and 6.

then i want it to do this

Please Enter the
<cfif myRand1 IS 1>
#myRand1#st
<cfelseif myrand1 IS 2>
#myRand1#nd
<cfelseif myrand1 IS 3>
#myRand1#rd
<cfelse>
#myRand1#th
</cfif>
Pin character

and the
<cfif myRand2 IS 1>
#myRand2#st
<cfelseif myrand2 IS 2>
#myRand2#nd
<cfelseif myrand2 IS 3>
#myRand2#rd
<cfelse>
#myRand2#th
</cfif>

i have not writen a componant / funtion before and want to learn so please can you help if you do a componant please can you explain how to invoke it and if its a function please can you show me how to use it...

thanks :)
0
Comment
Question by:Ken-doh
  • 8
  • 6
14 Comments
 
LVL 11

Expert Comment

by:hart
ID: 9817712
<CFFUNCTION NAME="getRandom">
    <cfset myRand1 =RandRange(1,6)>
    <cfset myRand2 =RandRange(1,6)>
    <cfloop condition="#myRand1# eq #myRand2#">
         <cfset myRand2 = RandRange(1,6)>
    </cfloop>
    Please Enter the
    <cfif myRand1 IS 1>
         #myRand1#st
     <cfelseif myrand1 IS 2>
          #myRand1#nd
     <cfelseif myrand1 IS 3>
         #myRand1#rd
     <cfelse>
          #myRand1#th
     </cfif>
     Pin character   and the
     <cfif myRand2 IS 1>
        #myRand2#st
     <cfelseif myrand2 IS 2>
        #myRand2#nd
     <cfelseif myrand2 IS 3>
       #myRand2#rd
     <cfelse>
       #myRand2#th
     </cfif>
</CFFUNCTION>


and call this function as
<cfoutput>#getRandom()#</cfoutput>

Regards
Hart
0
 
LVL 1

Author Comment

by:Ken-doh
ID: 9817861
thanks

but in the interest of reducing code could it be curr to 2 functions ??


one gets the random numbers the other adds  the "st" or "nd "or "th" etc etc

and then writing something like please enter the #firstnum# and the #secondnum#

rather than calling a line of text ???
0
 
LVL 1

Author Comment

by:Ken-doh
ID: 9818088
i was trying myself:

<CFFUNCTION NAME="getRandom" returntype="struct">
      <cfset var myStruct = structNew()>
    <cfset myStruct.myRand1 = RandRange(1,6)>
    <cfset myStruct.myRand2 = RandRange(1,6)>
    <cfloop condition='#myStruct.myRand1# eq #myStruct.myRand2#'>
         <cfset myStruct.myRand2 = RandRange(1,6)>
    </cfloop>
       <cfreturn myStruct>
</CFFUNCTION>

however it says

Complex object types cannot be converted to simple values.  and errors :(

prob something to do with: <cfloop condition='#myStruct.myRand1# eq #myStruct.myRand2#'>


any ideas??
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:Ken-doh
ID: 9818165
ok managing to fumble through this::


<CFFUNCTION NAME="getRandom" returntype="struct">
      <cfset myStruct = structNew()>
    <cfset myStruct.myRand1 = RandRange(1,6)>
    <cfset myStruct.myRand2 = RandRange(1,6)>
    <cfloop condition="#myStruct.myRand1# eq #myStruct.myRand2#">
         <cfset myStruct.myRand2 = RandRange(1,6)>
    </cfloop>
       <cfreturn myStruct>

</CFFUNCTION>

then calling

<cfset x=getRandom()>
then <cfoutput>#x.myRand2#</cfoutput>

now to do the rest of it... any ideas ??? need whats output from the random number to generate the correct string

i want it as a seperate funtion so i can reuse it :)
0
 
LVL 1

Author Comment

by:Ken-doh
ID: 9818299
right Now all i need to know is how to pass function params

i would normally go funtionname(1,2,3)

but u cant do this in CFFUNCTION  any ideas??

the i need to call the function with the params in it but it wont work why not?
0
 
LVL 1

Author Comment

by:Ken-doh
ID: 9818950
done it now :)

thanks for the initial help mate... can you give me this as a componant and the points are yours...

my functions:


<CFFUNCTION NAME="getrandom" output="false" returntype="struct" hint="generates 2 random numbers between 1 and 8">
      <cfset myStruct = structNew()>
    <cfset myStruct.myRand1 = RandRange(1,6)>
    <cfset myStruct.myRand2 = RandRange(1,6)>
    <cfloop condition="#myStruct.myRand1# eq #myStruct.myRand2#">
         <cfset myStruct.myRand2 = RandRange(1,6)>
    </cfloop>
      <cfreturn myStruct>

</CFFUNCTION>

<CFFUNCTION NAME="formatnumber" output="false" hint="formats a number between 1 and 8">
      <cfargument name="myVar" type="numeric" required="yes">
     <cfif myVar IS 1>
           <cfset myString ="#myVar#st">
     <cfelseif myVar IS 2>
        <cfset myString ="#myVar#nd">
     <cfelseif myVar IS 3>
        <cfset myString ="#myVar#rd">
     <cfelse>
        <cfset myString ="#myVar#th">
     </cfif>
       <cfreturn myString>
             
</CFFUNCTION>
0
 
LVL 11

Expert Comment

by:hart
ID: 9822552
i didn't post any replies becuse, i go offline by 19:30 indian time..
otherwise i would have given u all the suggestions...

basically its all simple and gr8 that u figured it out on ur own..

now about the component code just do this
</cfcomponent>
<CFFUNCTION NAME="getrandom" output="false" returntype="struct" hint="generates 2 random numbers between 1 and 8">
     <cfset myStruct = structNew()>
    <cfset myStruct.myRand1 = RandRange(1,6)>
    <cfset myStruct.myRand2 = RandRange(1,6)>
    <cfloop condition="#myStruct.myRand1# eq #myStruct.myRand2#">
         <cfset myStruct.myRand2 = RandRange(1,6)>
    </cfloop>
     <cfreturn myStruct>

</CFFUNCTION>

<CFFUNCTION NAME="formatnumber" output="false" hint="formats a number between 1 and 8">
     <cfargument name="myVar" type="numeric" required="yes">
     <cfif myVar IS 1>
          <cfset myString ="#myVar#st">
     <cfelseif myVar IS 2>
        <cfset myString ="#myVar#nd">
     <cfelseif myVar IS 3>
        <cfset myString ="#myVar#rd">
     <cfelse>
        <cfset myString ="#myVar#th">
     </cfif>
      <cfreturn myString>
           
</CFFUNCTION>
</cfcomponent>

and save this file as showRandow.cfc

now in the calling page do this to invoke it
<cfinvoke component="showRandow" method="getrandom" returnVariable="myStruct">
           <CFINVOKEARGUMENT name="selProduct" value="#selProduct#">
</CFINVOKE>

and
<CFOUTPUT>
<cfinvoke component="showRandow" method="formatnumber" returnVariable="myString">
           <CFINVOKEARGUMENT name="myVar" value="1">
</CFINVOKE>
#myString#
</CFOUTPUT>

Regards
Hart
0
 
LVL 11

Expert Comment

by:hart
ID: 9822586
oh sorry a small typo error in the above given thread

in this line
now in the calling page do this to invoke it
<cfinvoke component="showRandow" method="getrandom" returnVariable="myStruct">
           <CFINVOKEARGUMENT name="selProduct" value="#selProduct#">
</CFINVOKE>

use only
<cfoutput>
<cfinvoke component="showRandow" method="getrandom" returnVariable="myStruct"></CFINVOKE>
#myStruct#
</cfoutput>

Regards
Hart

0
 
LVL 1

Author Comment

by:Ken-doh
ID: 9823291
at the moment when I am calling it i am doing:

<cfset myPin=getrandom()>  - create the struct

then access it by:

#formatnumber(myPin.myRand2)#

how is this done when invoking?? the same???

0
 
LVL 11

Expert Comment

by:hart
ID: 9823346
i haven't checked it but try this directly

<cfoutput>
<cfinvoke component="showRandow" method="getrandom" returnVariable="myStruct"></CFINVOKE>
#myStruct.myRand2#
</cfoutput>

Regards
Hart

0
 
LVL 1

Author Comment

by:Ken-doh
ID: 9823393
yes that looks good, however could call the return variable pin??

<cfoutput>
<cfinvoke component="showRandow" method="getrandom" returnVariable="pin"></CFINVOKE>
#pin.myRand2#
</cfoutput>

would that work??

got to learn all this componant crap :(
0
 
LVL 11

Accepted Solution

by:
hart earned 300 total points
ID: 9823598
yes that will work tooo.
returnVariable value can be anything....

Regards
Hart
0
 
LVL 1

Author Comment

by:Ken-doh
ID: 9823655
thanks mate :)

a well earned 75 points
0
 
LVL 11

Expert Comment

by:hart
ID: 9823674
thnx mate :-)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month19 days, 6 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question