[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Good CF function code / componant

Posted on 2003-11-25
14
Medium Priority
?
268 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
Simplify Your Workload with One Tool

How do you combat today’s intelligent hacker while managing multiple domains and platforms? By simplifying your workload with one tool. With Lunarpages hosting through Plesk Onyx, you can:

Automate SSL generation and installation with two clicks
Experience total server control

 
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

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…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

650 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