Connecting Tech Pros Worldwide Help | Site Map

Testing for undefined properties

Michael McDowell
Guest
 
Posts: n/a
#1: Nov 16 '05
Hi

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

Ta in advance
Michael McD.
Jochen Kalmbach
Guest
 
Posts: n/a
#2: Nov 16 '05

re: Testing for undefined properties


=?Utf-8?B?TWljaGFlbCBNY0Rvd2VsbA==?= wrote:
[color=blue]
> How might I test for property being <undefined> before I try and use it?[/color]

if (property == null)
{
// undefined...
}

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server ?
http://sourceforge.net/projects/srvreport/
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#3: Nov 16 '05

re: Testing for undefined properties


Michael McDowell <michael.mcdowell@reuters.com> wrote:[color=blue]
> How might I test for property being <undefined> before I try and use it?[/color]

if (PropertyName==null)

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Stoitcho Goutsev \(100\) [C# MVP]
Guest
 
Posts: n/a
#4: Nov 16 '05

re: Testing for undefined properties


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

--
HTH
Stoitcho Goutsev (100) [C# MVP]


"Michael McDowell" <michael.mcdowell@reuters.com> wrote in message
news:F09274C1-28E0-49F6-B6DB-7A7984D71239@microsoft.com...[color=blue]
> Hi,
>
> How might I test for property being <undefined> before I try and use it?
>
> Ta in advance,
> Michael McD.[/color]


Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#5: Nov 16 '05

re: Testing for undefined properties


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.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com

"Michael McDowell" <michael.mcdowell@reuters.com> wrote in message
news:F09274C1-28E0-49F6-B6DB-7A7984D71239@microsoft.com...[color=blue]
> Hi,
>
> How might I test for property being <undefined> before I try and use it?
>
> Ta in advance,
> Michael McD.[/color]


Closed Thread