Solved

Good CF function code / componant

Posted on 2003-11-25
14
230 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
 
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
Give your grad a cloud of their own!

With up to 8TB of storage, give your favorite graduate their own personal cloud to centralize all their photos, videos and music in one safe place. They can save, sync and share all their stuff, and automatic photo backup helps free up space on their smartphone and tablet.

 
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 75 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

A web service (http://en.wikipedia.org/wiki/Web_service) is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now