|
Prev: Call for Papers with Extended Deadline: 2007 International Conference on Software Engineering Research and Practice (SERP'07), June 25-28, 2007, USA
Next: Scenario Frequency Battle (was: Relational-to-OOP Tax)
From: ssg31415926 on 26 Feb 2007 14:12 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
From: ssg31415926 on 28 Feb 2007 12:46
Thanks for your suggestion - I like your version better than mine! On 27 Feb, 16:54, "H. S. Lahman" <h.lah...(a)verizon.net> wrote: > 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... > > 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 > h...(a)pathfindermda.com > Pathfinder Solutionshttp://www.pathfindermda.com > blog:http://pathfinderpeople.blogs.com/hslahman > "Model-Based Translation: The Next Step in Agile Development". Email > i...(a)pathfindermda.com for your copy. > Pathfinder is hiring:http://www.pathfindermda.com/about_us/careers_pos3.php. > (888)OOA-PATH |