I'm having a little issue with string.format and the use of composite formatting to build a little string.
Here is an example:
x is basically an invalid character, and amongst other things x could be an &
string badString = x.ToString();
string error = string.Format(@"The variable name has invalid characters: ""{0}""", badString);
works fine if the invalid character is ! or " or £ well anything.... but the ampersand causes concatenation.
Example with a ^ as invalid character:
The variable name has invalid characters: "^"
With an ampersand:
The variable name has invalid characters: "" <----- I really want an & in there.
No matter what I try to do I just can't seem to get the "&" result.
I don't want to have to write an if statement to qualify what characters are in there if I can avoid it.
Thanks for your help :)
Loading
PaulaPosted May 17, 2007, 9:14 AM
I was using the string variable containing the & in an errorprovider's seterror method, as the feedback to the user.
I wanted to do this:
l_badString = "&";
error = string.Format("The variable name has invalid characters: \"{0}\"",l_badString);
errorProvider.SetError(this.nameTextBox, error)
ended up doing this:
errorProvider.SetError(this.nameTextBox, "The variable name has invalid characters: \"&&&\"")
I'm sure I'll figure out a better method. Annoying that I have to deal with & seperately from all other invalid characters.
Scott LyslePosted May 12, 2007, 4:36 AM
//char x = '&'; <= This works
string x = "&"; <= This also works
string badString = x.ToString();
//string badString = "&"; <= And this also works string error = string.Format(@"The variable name has invalid characters: ""{0}""", badString);
MessageBox.Show(error.ToString());
Set a breakpoint on the 'string error' line and check the value of badString. You might need to look at the origin of that value rather than the call shown in your example.