How do I determine if a property exists on an object in c#?

Originally posted by kalldrexx on November 16, 2011

I cannot seem to find a way to determine if a property exists or not. For example, some of the checkboxes in our Delphi application contain ValueChecked and ValueUnChecked properties, which determines the string value when the checkbox is checked or unchecked. I currently get these properties via

                valueChecked = (string)checkbox["ValueChecked"];                valueUnChecked = (string)checkbox["ValueUnChecked"];

The problem is that not all checkboxes use these properties, and the ones that don’t do not have these properties defined at all. So when this method is called in my code (generic method to handle setting the state of a checkbox) I get an error because TestComplete stalls waiting for the “ValueChecked” property, and when it’s not found and times out it returns an object that can’t be casted.

I need a way to instantly check if a property exists on an object so I can take an alternative action in those cases. Even if my current method would work (I just have to check if the returned object’s Exist property is true or false) it does not work for my case because the property check still has to wait until the timeout period before it determines that the property doesn’t exist.

I tried to find a Wait* method I could use (similar to WaitAliasChild) but all I could find was WaitProperty. However, that waits until the property is set to a specific value, and since I cannot predict what value the ValueChecked will be set to (in fact this code is meant to find that out) this does not work for me. I also tried using the componentObj.PropertyExists(Name), but this seems to fail as well as the VCLObject doesn’t have the PropertyExists method.

How can I efficiently and quickly determine if an object has a specific property?

Originally posted by tristaanogre on November 16, 2011

Have you tried aqObject.IsSupported?

Originally posted by kalldrexx on November 16, 2011

Nope. That appears to be what I need but I can’t seem to get it to work properly. When I try

if (!Connect.aqObject["IsSupported"](checkbox, "ValueChecked"))

an InvalidCastException occurs.

FYI, this is in C# via an open application communicating with TestComplete.

Originally posted by HKosova on November 17, 2011

Hi Matthew,

Try casting the IsSupported method result to boolean:


Originally posted by kalldrexx on November 18, 2011

That seems to have worked.

I didn’t think to try that as in almost all other situations it seems like if an object’s property (or method) returns true or false, I don’t have to explicitly cast to bool in if statements. I wonder why this situation is different.

Originally posted by HKosova on November 30, 2011

Hi Matthew,

When I try

if (!Connect.aqObject["IsSupported"](checkbox, "ValueChecked"))

an InvalidCastException occurs.

…

if an object’s property (or method) returns true or false, I don’t have to explicitly cast to bool in if statements. I wonder why this situation is different.

<<

The dev team have investigated the InvalidCastException issue. It’s a bug, and it’s going to be fixed in one of future releases. Thanks for bringing this to our attention!

Meanwhile, please use the explicit boolean cast as in my previous reply.

Originally posted by kalldrexx on November 30, 2011

Sweet, thanks for the reply!

Originally posted by HKosova on June 22, 2012

Hi Matthew,

Quick update: the InvalidCastException issue was fixed in TestComplete 9.