Creating Full Name from given, initials, sn, when some might be null
- From: "ssg31415926" <newsjunkmail@xxxxxxxxx>
- Date: 26 Feb 2007 11:12:16 -0800
I have three attributes: givenName, initials and sn (surname) - one or
more could be null but not all three. I have to create a displayName
attribute from as many of the three as are present, with spaces
separating them. E.g.
givenName = Bob
initials = <empty>
sn = Smith
should produce Bob Smith whereas
givenName = Lisa
initials = B
sn = Smith
should produce Lisa B Smith
Is there a better way of writing it than this, which feels clunky to
me (if the attribute is <null>, it's not present in the Properties
collection, hence checking using Count):
StringBuilder sb = new StringBuilder();
bool previousValueSet = false;
if (src[0].Properties["givenName"].Count > 0)
{
sb.Append(src[0].Properties["givenName"]
[0].ToString());
previousValueSet = true;
}
if (src[0].Properties["initials"].Count > 0)
{
if (previousValueSet)
{
sb.Append(" ");
}
sb.Append(src[0].Properties["initials"]
[0].ToString());
previousValueSet = true;
}
if (src[0].Properties["sn"].Count > 0)
{
if (previousValueSet)
{
sb.Append(" ");
}
sb.Append(src[0].Properties["sn"][0].ToString());
}
string fullName = sb.ToString();
SSG
.
- Follow-Ups:
- Re: Creating Full Name from given, initials, sn, when some might be null
- From: H. S. Lahman
- Re: Creating Full Name from given, initials, sn, when some might be null
- Prev by Date: Re: It's the Psychology, Stupid (Was: The reality of Topmind)
- Next by Date: Re: Relational-to-OOP Tax
- Previous by thread: Social networks
- Next by thread: Re: Creating Full Name from given, initials, sn, when some might be null
- Index(es):