Testing for undefined properties

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

How might I test for property being <undefined> before I try and use it

Ta in advance
Michael McD.
 
Hi Michael,

If the property resturns a reference type
if(foo.Property == null)
{
}

will do.

If it returns value type, though, there is no way, unless you don't keep
some flag whether the values state of the object is not the default one. BTW
C# 2 will introduce nullable value types, which can be handy in such cases
 
Michael,

Do you mean the value that the property returns, or if the property
exists? If it is the former, then you can just check the value, and compare
against null. However, if the return type is a value type (as others have
mentioned), then this is impossible, because value types can not be null.

However, if you want to see if an object implements a property, then you
can use the GetProperty method on the Type representing the type of the
object, checking the return value against null. If it is null, then the
property does not exist.

Hope this helps.
 
Back
Top