Solved

Good CF function code / componant

Posted on 2003-11-25
14
266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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

Turn your laptop into a mobile console!

The CV211 Laptop USB Console Adapter provides a direct Laptop-to-Computer connection for fast and easy remote desktop access with no software to install.

Question has a verified solution.

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

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…
What You Need to Know when Searching for a Webhost Provider
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

707 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