Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

using isDefined for a dynamic struct?

New Here ,
May 22, 2009 May 22, 2009

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

TOPICS
Getting started
1.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 22, 2009 May 22, 2009
<cfif StructKeyExists(Form, "your_field_name")>
....

Mack

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 22, 2009 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 22, 2009 May 22, 2009
<cfif StructKeyExists(Form, your_dynamic_property_name)>
....

?

Mack

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
May 22, 2009 May 22, 2009

I share Mack's confusion....

It is a basic programming concept that anywhere one can put a string, i.e. "myProperty" one can put a variable that contains a string.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
May 22, 2009 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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 22, 2009 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
May 22, 2009 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 22, 2009 May 22, 2009

This, "I'm populating form fields using a struct object that is created from a query", looks like it might be more complicated than necessary.  Given that populating form fields from queries is reasonably straighforward, what added value does your structure give you?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 23, 2009 May 23, 2009
LATEST
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.

Aha, there, is one source of confusion. The variable myStruct.myProperty may or may not exist. Whereas, myObject.myProperty always exists, and so there is no reason to check for its existence. The usual thing to do is to check for the existence of myStruct.myProperty before passing it as a parameter of a function.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 22, 2009 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 22, 2009 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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources