Optional Arguments in a CFC

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  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. Similar Questions and Discussions

    1. numberformat - is optional no longer optional?
      Hey all, we just switched to mx. mx 7 is on our horizon, but not close enough for me. I have a problem with numberformat that i did not have on...
    2. Optional properties
      If you have a data class, Person, with two properties, FirstName and LastName, and at a later stage you want to add a new optional property to the...
    3. WSDL and Optional Parameters
      Since there's no way to create a c# method with optional, or nullable parameters. And since you can't write an overloaded web method. Is it...
    4. Optional Parameters ASP to SQL stored procedure
      Hi there, Can someone help, this is driving me crazy I have written a stored procedure in sql with input, output and return parameters. This...
    5. preg_match_all optional subpattern
      Using preg_match_all, I need to capture a list of first and last names plus an optional country code proceeding them. For example: ...
  3. #2

    Default Re: Optional Arguments in a CFC

    You can specify the argument names:

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

  4. #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

  5. #4

    Default Re: Optional Arguments in a CFC

    Thanks guys!
    Cannikinn Guest

Posting Permissions

  • You may not post new threads
  • You may 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