473,320 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Setting Text property to null

The following (C#) code snippet sets the text property of a label to null,
then tests it for null, only to find out that is isn't. Can anyone explain
why?

Label1.Text = null;
if (Label1.Text == null) {
Label2.Text = "Is null";
} else {
Label2.Text = "Is not null";
}

Jul 21 '05 #1
4 1934
The short answer is - if you check in your debugger - when you set it to
null it is actually set to "".
"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:E3**********************************@microsof t.com...
The following (C#) code snippet sets the text property of a label to null,
then tests it for null, only to find out that is isn't. Can anyone
explain
why?

Label1.Text = null;
if (Label1.Text == null) {
Label2.Text = "Is null";
} else {
Label2.Text = "Is not null";
}

Jul 21 '05 #2
Well I noticed that it is set to "". However, it works fine if you use a
string variable. I realize that there is a difference between a string
property, such as Text and a string variable, it just seems to me they should
behave more similarly.
--Mike

"Jediah L." wrote:
The short answer is - if you check in your debugger - when you set it to
null it is actually set to "".
"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:E3**********************************@microsof t.com...
The following (C#) code snippet sets the text property of a label to null,
then tests it for null, only to find out that is isn't. Can anyone
explain
why?

Label1.Text = null;
if (Label1.Text == null) {
Label2.Text = "Is null";
} else {
Label2.Text = "Is not null";
}


Jul 21 '05 #3
some properties have code under them... apparently, in this case, if you
attempt to set the Text property to a null, the code forces the value to
String.Empty.

It makes sense. The Text property of a control can't really be null. Null
is not a value. It means that "no value is known". Since the control has
to render the value, it makes sense that it would require you to provide
one.

Be happy you didn't get a runtime error.

--- Nick

"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:F1**********************************@microsof t.com...
Well I noticed that it is set to "". However, it works fine if you use a
string variable. I realize that there is a difference between a string
property, such as Text and a string variable, it just seems to me they should behave more similarly.
--Mike

"Jediah L." wrote:
The short answer is - if you check in your debugger - when you set it to
null it is actually set to "".
"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:E3**********************************@microsof t.com...
The following (C#) code snippet sets the text property of a label to null, then tests it for null, only to find out that is isn't. Can anyone
explain
why?

Label1.Text = null;
if (Label1.Text == null) {
Label2.Text = "Is null";
} else {
Label2.Text = "Is not null";
}


Jul 21 '05 #4
Yup, I guess there must be code back there that makes sure Text is never
null. Thanks for your comments.

"Nick Malik" <ni*******@hotmail.nospam.com> wrote in message
news:g0AYc.333188$a24.112462@attbi_s03...
some properties have code under them... apparently, in this case, if you
attempt to set the Text property to a null, the code forces the value to
String.Empty.

It makes sense. The Text property of a control can't really be null. Null is not a value. It means that "no value is known". Since the control has
to render the value, it makes sense that it would require you to provide
one.

Be happy you didn't get a runtime error.

--- Nick

"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:F1**********************************@microsof t.com...
Well I noticed that it is set to "". However, it works fine if you use a
string variable. I realize that there is a difference between a string
property, such as Text and a string variable, it just seems to me they

should
behave more similarly.
--Mike

"Jediah L." wrote:
The short answer is - if you check in your debugger - when you set it to null it is actually set to "".
"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:E3**********************************@microsof t.com...
> The following (C#) code snippet sets the text property of a label to

null, > then tests it for null, only to find out that is isn't. Can anyone
> explain
> why?
>
> Label1.Text = null;
> if (Label1.Text == null) {
> Label2.Text = "Is null";
> } else {
> Label2.Text = "Is not null";
> }
>


Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: | last post by:
Hi, I am setting the NumericUpDown .Value property and the ValueChanged event is NOT being fired. Does this ONLY get fired when I change it on the UI and not programatically? Thanks
21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
0
by: Praveen | last post by:
Hello. I am writing some code that accepts a DFS Link and Username and grants that User permissions to the physical directory that the DFS Link corresponds to. I am using the System.Management...
2
by: MLH | last post by:
Combo box LimitToList property setting of No does not prevent the MS Access error "The text you enter must match an entry in the list" from popping up. What makes this bad is that the error is not...
10
by: MLH | last post by:
I have an A97 table with a Yes/No field named TowJob and a form bound to that table. The TowJob control on the form is bound to the same field. It is an option group with Yes and No bttns valued...
18
by: Dixie | last post by:
Can I set the Format property in a date/time field in code? Can I set the Input Mask in a date/time field in code? Can I set the Format of a Yes/No field to Checkbox in code? I am working on...
6
by: Mike | last post by:
The following (C#) code snippet sets the text property of a label to null, then tests it for null, only to find out that is isn't. Can anyone explain why? Label1.Text = null; if (Label1.Text...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
4
by: David Veeneman | last post by:
I'm creating a UserControl that uses a LinkLabel. For reasons that I won't bore everyone with, I don't want the LinkLable to show the default hand cursor when the mouse enters the control. Should...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.