473,379 Members | 1,511 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,379 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 1940
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.