That looks great Nigel thanks.
And the null-coalescing operator checks both null and empty?
From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of
Nigel Witherdin
Sent: Wednesday, 19 October 2011 12:42 PM
To: OzMoss
Subject: RE: Check null or empty on it field
Rather than pull the title out to a seperate string variable and then check on
it, I would use something like:
string iTitle = (string)(spListItem[Title] ?? No Title);
string iDesc = (string)(spListItem[Description] ?? No description);
I believe casting the field value to string is more efficient then using a
ToString function, and using the null-coalescing operator is more efficient
then a seperate if statement (though I am prepared to be shouted down over
this :)
Also, I think it is a lot neater in the code, and the same form can be used for
different datatypes:
DateTime someDate = (DateTime)(item[Date Field Name] ?? DateTime.MinValue);
Cheers,
Nigel
From: paul.no...@ceosyd.catholic.edu.au
To: ozmoss@ozmoss.com
Date: Wed, 19 Oct 2011 11:10:58 +1100
Subject: Check null or empty on it field
Hi all,
I'm trying to put some checks in place to avoid exceptions when a field has no
value. I had tried null but it didn't seem to be enough. I'm now trying the
following but continue to get an exception when a field in any item is empty.
Is there a more robust way to do this? Check length as well, or instead??
// Check the value for each string and supply a defualt if none exists
if (string.IsNullOrEmpty(iTitle))
{
iTitle = No Title;
}
if (string.IsNullOrEmpty(iDesc))
{
iDesc = No description;
}
Kind regards,
Paul Noone
---
Online Developer/SharePoint Administrator
Infrastructure Team, ICT
Catholic Education Office, Sydney
p: (02) 9568 8461
f: (02) 9568 8483
e: paul.no...@ceosyd.catholic.edu.aumailto:paul.no...@ceosyd.catholic.edu.au
w: http://www.ceosyd.catholic.edu.au/
___ ozmoss mailing list
ozmoss@ozmoss.commailto:ozmoss@ozmoss.com
http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
___
ozmoss mailing list
ozmoss@ozmoss.com
http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss