1

What I am trying to achieve is to merge three strings. Two are provided as strings; firstname and lastname, while the third is a simple comma/space separator. Given the following lines of code:

//Working code
var sep = ", ";
var fullName = myNewBO[0].LastName + sep + myNewBO[0].FirstName;

//Erronous code
var fullName = myNewBO[0].LastName + ", " + myNewBO[0].FirstName;

The string is returned to a cell in a DataGridView. While the first bit of code performs as expcted the latter does not. The string does not show in the cell as expected. Can someone tell me why the latter does not work? Also if you have a better solution to the problem please provide one.

EDIT: Solved. As suspected, and pointed out by several answers the problem was elsewhere in my code and the two alternatives do the exact same thing. Thanks for the syntax suggestions though :)

7
  • Why doesn't it work? Did you get an error? Commented Jan 26, 2009 at 15:58
  • Also, is there a reason you're using implicit typing with "var" rather than strongly typing these as strings? Commented Jan 26, 2009 at 16:00
  • @Ian: Note that implicit typing is still strong typing. It's just not explicit. Commented Jan 26, 2009 at 16:07
  • @Josh: made edit to original post to provide more details on this. @Ian: Yes, to get rid of ReSharper warnings, I am used to implicit typing myself, but for some reason ReSharper does not like it, and I've been told to try to follow ReSharper when it makes sense. dont know if thats the case here. Commented Jan 26, 2009 at 16:09
  • Another note: you could make the ReSharper's suggestion to use implicit typing as Hint, and it will stop showing up as an orange warning. Commented Jan 26, 2009 at 16:16

5 Answers 5

8

I prefer using string.Format("{0}, {1}",myNewBO[0].LastName,myNewBO[0].FirstName)

Now you can abstract out the format string if you want it be "First Last" for example you can use a different formatting string.

Edit

In response to your actual error, I like others here don't see what is wrong the line of code you have should work so the question becomes: "How are you binding this value to the grid?"

Are you doing this in an Eval() or code behind etc....

One suggestion would to add a ToString(string) method which takes a format string in, then you can bind to the evaluation of the method. And should your business requirements change you just change the formatting string.

Sign up to request clarification or add additional context in comments.

2 Comments

As a side note I had intentionaly swapped the LastName & FirstName since First is First it would go in position {0}. Not a big deal but personal preference.
Thanks... I'll be sure to remember this syntax in the future. The error was elsewhere though :P
5
string.Join(sep, new string[] {myNewBO[0].LastName, myNewBO[0].FirstName});

1 Comment

iirc this is faster, but format is certainly more obviously readable
1

I suspect you have something else happening in your code, and you're mistaking where the error is occurring. I can't for the life of me see why those two would behave differently at all. I suggest you log the value after the assignment for both cases - I'm sure you'll find they're the same.

2 Comments

I agree... Just wanted to make sure.
They both work as expected now, so the error was obviously elsewhere
1

There is really no difference in your two calls, I see no error. What exception are you getting. Joel Coehoorn's answer with regards to String.Join is perfect for what you need.

1 Comment

I agree with your reasoning, however the two do not provide the same result for some reason. The error may be elsewhere or pepkac... :P
0

What error is it throwing? That could tell you alot about why it is crashing.

Your calls both look valid on the surface to me. I would suggest that you make sure LastName and FirstName are strings and not null. To be sure, I guess you could append .ToString() to the end of FirstName and LastName.

2 Comments

Calling ToString() on a null object would result in a NullReferenceException. There is no point in calling ToString() on strings themselves and Sakkle would be able to determine for himself if they were strings from intellisense.
It doesn't crash, it just didn't provide the same end result. It does now however, so the error obviously was elsewhere

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.