Re: Creating Full Name from given, initials, sn, when some might be null
- From: "H. S. Lahman" <h.lahman@xxxxxxxxxxx>
- Date: Tue, 27 Feb 2007 16:54:29 GMT
Responding to Ssg31415926...
This forum is usually concerned with OOA/D issues rather that tactical coding practices in particular OOPLs, so you might get a better answer in the appropriate language or programming forum. But as long as we're here...
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
You could make it more compact by making the string operations generic (using appropriate syntax for whatever language you are using):
static String nameIdentifiers [3] = {"GivenName", "initials", "sn"};
StringBuilder sb = new StringBuilder();
bool previousValueSet = FALSE;
for (i = 0; i < 3; i++)
{
if (src[0].Properties[nameIndentifiers[i]].Count > 0)
{
if (previousValueSet)
sb.Append(" ");
sb.Append(src[0].Properties[nameIdentifiers[i]][0].ToString());
previousValueSet = TRUE;
}
}
*************
There is nothing wrong with me that could
not be cured by a capful of Drano.
H. S. Lahman
hsl@xxxxxxxxxxxxxxxxx
Pathfinder Solutions
http://www.pathfindermda.com
blog: http://pathfinderpeople.blogs.com/hslahman
"Model-Based Translation: The Next Step in Agile Development". Email
info@xxxxxxxxxxxxxxxxx for your copy.
Pathfinder is hiring: http://www.pathfindermda.com/about_us/careers_pos3.php.
(888)OOA-PATH
.
- Follow-Ups:
- Re: Creating Full Name from given, initials, sn, when some might be null
- From: ssg31415926
- Re: Creating Full Name from given, initials, sn, when some might be null
- References:
- Creating Full Name from given, initials, sn, when some might be null
- From: ssg31415926
- Creating Full Name from given, initials, sn, when some might be null
- Prev by Date: Re: Relational-to-OOP Tax
- Next by Date: Re: Question About Builder Object
- Previous by thread: Creating Full Name from given, initials, sn, when some might be null
- Next by thread: Re: Creating Full Name from given, initials, sn, when some might be null
- Index(es):