How to test for objects property that does not exist

  • Thread starter Thread starter Steve Montague
  • Start date Start date
S

Steve Montague

How does one test to insure a property of an objects exists?

The debugger shows the object.property as: "error: 'object.property' does not exist"

using: if (object.property != null) results in: "Object reference not set to an instance of an object."

Thanks for help!
 
You can use reflection to do it safely, here is a snip

----------------------------
bob b = new bob();

Type t = b.GetType();
PropertyInfo p = t.GetProperty("FName");
if (p == null)
{
// property does not exist
}
else
{
// property exists
}

--------------------------

How does one test to insure a property of an objects exists?

The debugger shows the object.property as: "error: 'object.property' does not exist"

using: if (object.property != null) results in: "Object reference not set to an instance of an object."

Thanks for help!
 
Hi Steve,

Thank you for posting in the community!

In Visual Studio.Net, both edting-time debugger and intellisense use the
Reflection to do validation and prompt.

.Net Reflection will query the meta of the assembly to get the information
of the classes in this assembly. It is a powerful tool in .Net.

In Bob's reply, he has told you how to get what you want through
Reflection.

.Net gives you many more feature and functions, for a quickstart of
Reflection, please refer to:
http://samples.gotdotnet.com/quickstart/howto/

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Steve Montague said:
How does one test to insure a property of an objects exists?

The debugger shows the object.property as: "error:
'object.property' does not exist"

using: if (object.property != null) results in:
"Object reference not set to an instance of an object."

That suggests that it's not that the property doesn't exist, but that
your reference is to null (rather than to a real object) in the first
place.

If the property didn't exist within the type, your code wouldn't
compile to start with.
 
Back
Top