Solved

Good CF function code / componant

Posted on 2003-11-25
14
216 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
Comment Utility
<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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 11

Expert Comment

by:hart
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
yes that will work tooo.
returnVariable value can be anything....

Regards
Hart
0
 
LVL 1

Author Comment

by:Ken-doh
Comment Utility
thanks mate :)

a well earned 75 points
0
 
LVL 11

Expert Comment

by:hart
Comment Utility
thnx mate :-)
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 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

6 Experts available now in Live!

Get 1:1 Help Now