Operator '&&' cannot be applied to operands of type 'string' and 'string'
I'm totally new to C# so your help is much appreciated.
Here's what I have.
string strSummary;
if(txtPhnNumber.Text != "" ){
strSummary += "\nPhone Number: " && txtPhnNumber.Text;
}
The error comes back saying, "
Operator '&&' cannot be applied to operands of type 'string' and 'string'" whe rebuilding the site. I'm using Visual Web Developer Express edition.
n0n4m3 wrote: |
Hi, if you want to "add" (join) two strings you need to use the operator +: strSummary += "\nPhone Number: " + txtPhnNumber.Text;
|
|
Just FYI, joining two strings together is called "concatenating" the strings. Also, since C# strings are immutable, you're better off using a StringBuilder object to do this.
Example:
using System.Text;
...
const string phoneNumberLiteralText = "\nPhone Number: ";
StringBuilder summary = new StringBuilder();
<Loop construct here>
summary.Append(phoneNumberLiteralText);
summary.Append(txtPhnNumber.Text);
</Loop construct>
You want to concat some string, you can do this in many ways. Concatting can be really expencive when you work with large strings or when you have a lot to concat. I normally use a StringBuilder when i have more then 5 concats.
For your code the easiest solution is to use the + operator to append the first string to the second string and the += operator to append this result to the strSummary:
| | string strSummary;if(txtPhnNumber.Text != "" ) { strSummary += "\nPhone Number: " + txtPhnNumber.Text; } |