Skip to main content
Participating Frequently
May 22, 2009
Question

using isDefined for a dynamic struct?

  • May 22, 2009
  • 4 replies
  • 1966 views

I know how you can check for a defined variable or object using isDefined("myVar"), but I have a struct that can have varying properties. While I can explicitly write out the "myStruct.myProperty" checks in quotes for this method, I'd like to put these into a function where a variable is passed into it.

Is this possible? Basically, I'm populating form fields and some fields may not be defined yet. Thoughts? Thanks.

Ryan

This topic has been closed for replies.

4 replies

Participating Frequently
May 22, 2009

Thanks for the help BTW. I'm still new to CF, I'm more of a .net guy.

Basically, I have this. It's a form that edits and adds bios for members:

objStatement = objConnection.PrepareStatement("select bio_first_name, bio_last_name from cpppeBios where bio_id = ?");
objStatement.setInt(1, Int(FORM.edit_bio_id));
                   
objResults = CreateObject("java", "coldfusion.sql.QueryTable").Init(objStatement.ExecuteQuery());
               
EditBio = structNew();
EditBio = objResults;

....and, I have input fields down below...

<input type='text' value='' />

I use the same form for adding and editing bios. What is the best approach for handling populating of these fields when the EditBio object can be null or contain data. I would access the data using EditBio.bio_first_name, etc.

Thanks again.

Inspiring
May 22, 2009

To find infomation on specfic cold fusion tags, google "<cfNameOfTag> x".  For cold fusion functions, google "coldfusion functionname x".  In both cases x is the version number and is optional.  Invariably the 1st result will be the page of the cfml reference manual you want.

The cold fusion tag that will simplify what you are attempting is <cfquery>

Participating Frequently
May 22, 2009

Not sure if these are it.

I'm populating form fields using a struct object that is created from a query. My goal is when outputting the values into each element, I'm passing  myObject.myProperty (no quotes, but the actual object.property) into a function, check that it exists, and then write the value.

How can I go about this? Can I convert the name of the property to a string in order for me to test? It seems that these functions rely on getting a string name to test and can't test on the actual variable. Thanks again.

ilssac
Inspiring
May 22, 2009

If you are passing myObject.myProperty into a function inside the function is too late to test that myProperty exists in myObject.  If it does not exist the call to the function is going to fail.  Are you really wanting to test if myOjbect.myProperty is "empty" or are you really trying to test if myObject does or does not have a property named "myProperty"?

Maybe a bit of code that you have tried to use to achieve your goal would clarify what you are really trying to do.

ilssac
Inspiring
May 22, 2009

I think you are either looking for the #structKeyExists(myStruct,"myProperty")# or arrayNotation i.e. myStruct["myProperty"] which of course can be a varaible.

<cfset node = "myProperty">

<cfoutput>#myStruct[node]#<cfoutput>

Participating Frequently
May 22, 2009

....
]]>

Mack

Participating Frequently
May 22, 2009

thanks, but it still involves putting the property in quotes. Is there a way to check if myObj.someDynamicProperty exists without having to put it in quotes? Thanks.

Participating Frequently
May 22, 2009

....
]]>

?

Mack