By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,295 Members | 1,552 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,295 IT Pros & Developers. It's quick & easy.

How to test for objects property that does not exist

P: n/a
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!
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
}

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

"Steve Montague" <st************@getronics.com> wrote in message news:#q**************@TK2MSFTNGP12.phx.gbl...
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!
Nov 15 '05 #2

P: n/a

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.

Nov 15 '05 #3

P: n/a
Steve Montague <st************@getronics.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.