473,765 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2000
The short answer is - if you check in your debugger - when you set it to
null it is actually set to "".
"Mike" <Mi**@discussio ns.microsoft.co m> wrote in message
news:E3******** *************** ***********@mic rosoft.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**@discussio ns.microsoft.co m> wrote in message
news:E3******** *************** ***********@mic rosoft.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**@discussio ns.microsoft.co m> wrote in message
news:F1******** *************** ***********@mic rosoft.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**@discussio ns.microsoft.co m> wrote in message
news:E3******** *************** ***********@mic rosoft.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*******@hotm ail.nospam.com> wrote in message
news:g0AYc.3331 88$a24.112462@a ttbi_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**@discussio ns.microsoft.co m> wrote in message
news:F1******** *************** ***********@mic rosoft.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**@discussio ns.microsoft.co m> wrote in message
news:E3******** *************** ***********@mic rosoft.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
9922
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
3988
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 nothing in IE. I'd be greatful for any assistance. Also, if I will have problems the code on Opera or Safari, I'd appreciate any pointers--I don't have a Mac to test Safari. THanks very much, Michael
0
2339
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 namespace and WMI queries. When I run the code below and check the Security tab of the folder , I find that all entries have been cleared – which is ok and this is expected, main problem is that the User that I have set up the Trustee and ACE...
2
2168
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 a capturable error. My list has 2 fields: and . The 2nd column is the bound column. Here's the RowSource property setting: Select , From ;
10
2762
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 at -1 and 0 respectively. Unless I specifically set the table field's defaultvalue to Null, the form comes up with an automatic value of 0 in the control? The control has NO default value property setting. Why is that?
18
18417
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 a remote update of tables and fields and can't find enough information on these things. Also, how do you index a field in code?
6
248
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 == null) { Label2.Text = "Is null"; } else { Label2.Text = "Is not null"; }
1
6508
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" setting to be "E_ALL", notices are still not getting reported. The perms on my file are 664, with owner root and group root. The php.ini file is located at /usr/local/lib/php/php.ini. Any ideas why the setting does not seem to be having an effect? ...
4
6582
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 be a simple setting on the LinkLabel.Cursor property, right? Not so simple. I set the property, but still got the default 'hand' cursor. To debug, I added the following event handler to my LinkLabel control: private void...
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10164
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5277
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.