4

I have a function that has these arguments:

 <cffunction name="Agregar" access="public" returntype="any">
        <cfargument name="id_examen" type="numeric" required="yes" />
        <cfargument name="id_tema" type="numeric" required="yes" />
        <cfargument name="id_tipopregunta" type="numeric" required="yes" />
        <cfargument name="id_dificultad" type="numeric" required="yes" />
        <cfargument name="opciones_arreglo" type="array" required="no" />

I'm trying to use cfif to say what to do when the argument "opciones_arreglo" is not received. I used a function called isDefined, but it doesn't work. I just want to make the code run the else section, but I got this complaint about something that is obvious:

Element OPCIONES_ARREGLO is undefined in ARGUMENTS.


The error occurred in C:\www\htdocs\RHRR\componentes\bro\preguntas.cfc: line 24
22 :         <cfdump var="#Arguments#">
23 :         
24 :        **<cfif IsDefined(Arguments.opciones_arreglo)>**
25 :            
26 :             <cfinvoke method         = "RSAgregar"
Leigh
  • 28,765
  • 10
  • 55
  • 103
Mr_LinDowsMac
  • 2,644
  • 9
  • 56
  • 75
  • 7
    You missed the quotes around the variable name: `IsDefined("Arguments.opciones_arreglo")`. Also consider `StructKeyExists(Arguments,"opciones_arreglo")`. Docs: [isDefined](http://cfdocs.org/isdefined) [StructKeyExists](http://cfdocs.org/structkeyexists) – Peter Boughton Nov 15 '13 at 23:34

2 Answers2

5

The problem were the quotes. Beginner mistake :(

<cfif IsDefined("Arguments.opciones_arreglo")>
Mr_LinDowsMac
  • 2,644
  • 9
  • 56
  • 75
  • 2
    Yep, [forgetting the quotes is a common gotcha](http://stackoverflow.com/questions/10921081/why-a-parameter-is-not-defined-if-i-can-dump-it-and-it-has-a-value-in-coldfusio/10921421#10921421). However, I second Peter's suggestion of using `structKeyExists` instead. Primarily because it has greater precision than `isDefined`. – Leigh Nov 16 '13 at 00:55
1

You could add in a default value and it will always exist

<cfargument name="opciones_arreglo" type="array" required="no" default="#ArrayNew(1)#" />
James A Mohler
  • 11,060
  • 15
  • 46
  • 72
  • 1
    Default values may or may not be appropriate for function arguments. It depends on what's being done with the arguments and this question does not specify that. – Dan Bracuk Nov 16 '13 at 13:52