Professional Web Applications Themes

Optional Arguments in a CFC - Coldfusion - Advanced Techniques

Let's say I have the following function in a CFC: <cffunction name="init" access="public" output="false"> <cfargument name="id" type="numeric" required="true"> <cfargument name="name" type="string" required="false" default=""> <cfargument name="age" type="numeric" required="false" default="0"> ... </cffunction> Now, if I instantiate this object using <cfobject> (rather than <cfinvoke) is there a way to call this method and pass in the id and age, but not the name? Or do they have to appear in that order? In other words, is there a way to define which arguments you're passing in as the optional ones? For example: <cfobject component="com.cfcs.user" name="User"> <cfset User.init(13,24)> I know that with <cfinvoke> you ...

  1. #1

    Default Optional Arguments in a CFC

    Let's say I have the following function in a CFC:

    <cffunction name="init" access="public" output="false">
    <cfargument name="id" type="numeric" required="true">
    <cfargument name="name" type="string" required="false" default="">
    <cfargument name="age" type="numeric" required="false" default="0">
    ...
    </cffunction>

    Now, if I instantiate this object using <cfobject> (rather than <cfinvoke) is
    there a way to call this method and pass in the id and age, but not the name?
    Or do they have to appear in that order? In other words, is there a way to
    define which arguments you're passing in as the optional ones? For example:

    <cfobject component="com.cfcs.user" name="User">
    <cfset User.init(13,24)>

    I know that with <cfinvoke> you can actually say which are which using
    <cfinvokeargument>, but there are times when I'd like to create an object so
    that I can call it several times on the same page without the overhead of
    constantly invoking the component over and over.

    I believe Java does this (I'm not sure of the syntax), but Ruby does it like
    so:

    User.init(:id => 13, :age => 24)

    Cannikinn Guest

  2. #2

    Default Re: Optional Arguments in a CFC

    You can specify the argument names:

    <cfset User.init(id=13,age=24)>
    TA-Selene Guest

  3. #3

    Default Re: Optional Arguments in a CFC

    Or you can build a structure of arguments.

    <cfset args = structNew()>
    <cfset args.id = 13>
    <cfset args.age = 24>
    <cfset User.init(args)>

    Jesse Monson

    Sfumato Guest

  4. #4

    Default Re: Optional Arguments in a CFC

    Thanks guys!
    Cannikinn Guest

Similar Threads

  1. numberformat - is optional no longer optional?
    By miki in forum Macromedia ColdFusion
    Replies: 1
    Last Post: April 18th, 11:07 PM
  2. Optional properties
    By Alex Dinu in forum ASP.NET Web Services
    Replies: 4
    Last Post: December 21st, 04:25 PM
  3. WSDL and Optional Parameters
    By WHY in forum ASP.NET Web Services
    Replies: 16
    Last Post: August 10th, 08:47 AM
  4. preg_match_all optional subpattern
    By Han in forum PHP Development
    Replies: 5
    Last Post: October 7th, 05:50 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139