Is this valid C# syntax?
tboxUserName.Text = "This is silly";
if (tboxUserName.Text == NULL)
{
tboxUserName.Text = tboxUserName.Text + "Very Very silly!";
}
Can you add to the contents of a field in the above way?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Roy S194 reputationPosted Jan 30, 2013, 8:49 AM EST
richard smithRank #8,268 · 225 reputationPosted Jan 30, 2013, 8:51 AM EST
VulpesRank #55 · 29.2K reputationPosted Jan 30, 2013, 8:54 AM EST
1. You wouldn't test a string for nullness immediately after setting it to a string literal!
2. The Text property of controls is never 'naturally' null in any case - it's default value is an empty string.
3. If you concatenate a null string with a non-null string, the former is treated as if it were empty and so, if you know it's null, concatenation is a pointless operation.
However, there's nothing wrong with setting the Text property of a control to its existing value concatenated with one or more non-null strings. This is quite a common operation.
richard smithRank #8,268 · 225 reputationPosted Jan 30, 2013, 8:57 AM EST
So for example sake...
tboxUserName.Text = "This is silly";
tboxUserName.Text = tboxUserName.Text + "Very Very silly!";
My first value is getting overwritten by my 2nd value?
VulpesRank #55 · 29.2K reputationPosted Jan 30, 2013, 9:03 AM EST
richard smithRank #8,268 · 225 reputationPosted Jan 30, 2013, 9:14 AM EST
Anywho, thank you for confirming that is valid C# syntax.