Discussion:
Can TextBox.Text ever be null?
(too old to reply)
Jeff Johnson
2007-03-29 20:09:12 UTC
Permalink
Subject pretty much says it all. I was wondering if it was EVER necessary
for me to do this:

string myString = TextBox1.Text;

if (myString == null)
{
// Will this code EVER execute?
[...]
}
Herfried K. Wagner [MVP]
2007-03-29 21:31:59 UTC
Permalink
Post by Jeff Johnson
Subject pretty much says it all. I was wondering if it was EVER necessary
string myString = TextBox1.Text;
if (myString == null)
{
// Will this code EVER execute?
[...]
}
I have never tried to assign a 'null' reference, but if this is possible and
the property value gets changed, then it would be a possible value.
However, I have never seen that to occur.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Jeff Johnson
2007-03-30 13:17:29 UTC
Permalink
Post by Herfried K. Wagner [MVP]
Post by Jeff Johnson
Subject pretty much says it all. I was wondering if it was EVER necessary
string myString = TextBox1.Text;
if (myString == null)
{
// Will this code EVER execute?
[...]
}
I have never tried to assign a 'null' reference, but if this is possible
and the property value gets changed, then it would be a possible value.
However, I have never seen that to occur.
I suppose I should simply test it. Personally, if I had written the wrapper
for the text box, I would have intercepted the attempt to set Text to null
and turned it into String.Empty. Let's see if the MS developers thought the
same....
Rad [Visual C# MVP]
2007-03-30 17:59:02 UTC
Permalink
Post by Jeff Johnson
Subject pretty much says it all. I was wondering if it was EVER necessary
string myString = TextBox1.Text;
if (myString == null)
{
// Will this code EVER execute?
[...]
}
Hmm ... interesting question. From some informal tests i've done, no. I've
put the following code in an event

txt.Text=null;
if(txt.Text==null)
MessageBox.Show("It is null");
else
MessageBox.Show("it ain't");

The second MessageBox always shows
--
Bits.Bytes
http://bytes.thinkersroom.com
Aaron
2007-04-02 19:37:20 UTC
Permalink
Yes - "TextBox1.Text" can be equal to nothing. See below.

If TextBox1.Text.ToString.Length = 0 Then
' The textbox is empty.
End If

Hope this helps.
Aaron
Post by Jeff Johnson
Subject pretty much says it all. I was wondering if it was EVER necessary
string myString = TextBox1.Text;
if (myString == null)
{
// Will this code EVER execute?
[...]
}
Earl
2007-04-03 11:13:31 UTC
Permalink
Ahhhh ... but String.Empty is not Nothing.
Post by Aaron
Yes - "TextBox1.Text" can be equal to nothing. See below.
If TextBox1.Text.ToString.Length = 0 Then
' The textbox is empty.
End If
Hope this helps.
Aaron
Post by Jeff Johnson
Subject pretty much says it all. I was wondering if it was EVER necessary
string myString = TextBox1.Text;
if (myString == null)
{
// Will this code EVER execute?
[...]
}
Aaron
2007-04-03 13:36:44 UTC
Permalink
As I understood the initial question was how to tell if the ".Text" property
was empty or contained text. Checking the length of the ".Text" property
validates whether or not something has been entered in the TextBox.
Post by Earl
Ahhhh ... but String.Empty is not Nothing.
Post by Aaron
Yes - "TextBox1.Text" can be equal to nothing. See below.
If TextBox1.Text.ToString.Length = 0 Then
' The textbox is empty.
End If
Hope this helps.
Aaron
Post by Jeff Johnson
Subject pretty much says it all. I was wondering if it was EVER
string myString = TextBox1.Text;
if (myString == null)
{
// Will this code EVER execute?
[...]
}
Herfried K. Wagner [MVP]
2007-04-03 18:29:34 UTC
Permalink
Post by Aaron
As I understood the initial question was how to tell if the ".Text"
property was empty or contained text. Checking the length of the ".Text"
property validates whether or not something has been entered in the
TextBox.
A zero-length string ('String.Empty' or '""') is not the same as a
'Nothing'/'null' reference.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
RobinS
2007-04-04 04:52:40 UTC
Permalink
Post by Herfried K. Wagner [MVP]
Post by Aaron
As I understood the initial question was how to tell if the ".Text"
property was empty or contained text. Checking the length of the ".Text"
property validates whether or not something has been entered in the
TextBox.
A zero-length string ('String.Empty' or '""') is not the same as a
'Nothing'/'null' reference.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Although, if you try this, it returns true:

Debug.Print(String.Empty = Nothing)

And so does this:

Dim aString As String = ""
Debug.Print(a = Nothing)

Robin S.
Herfried K. Wagner [MVP]
2007-04-04 16:21:13 UTC
Permalink
Post by RobinS
Post by Herfried K. Wagner [MVP]
Post by Aaron
As I understood the initial question was how to tell if the ".Text"
property was empty or contained text. Checking the length of the ".Text"
property validates whether or not something has been entered in the
TextBox.
A zero-length string ('String.Empty' or '""') is not the same as a
'Nothing'/'null' reference.
Debug.Print(String.Empty = Nothing)
Dim aString As String = ""
Debug.Print(a = Nothing)
Yes, that's because you are using '=' for comparison, which compares for
equality and not for identity (as 'Is' does).
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Jeff Johnson
2007-04-03 20:42:31 UTC
Permalink
Post by Aaron
As I understood the initial question was how to tell if the ".Text"
property was empty or contained text.
Nope, that was not the question. The question was null/Nothing vs.
String.Empty, as others suggested.
Bob Powell [MVP]
2007-04-12 18:36:59 UTC
Permalink
You can attempt to assign null to any string. However, in this case the
value of String.Empty is used instead so you can certainly assign null
to a textbox Text property but next time you look at it it'll be
string.Empty.
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
Post by Jeff Johnson
Subject pretty much says it all. I was wondering if it was EVER necessary
string myString = TextBox1.Text;
if (myString == null)
{
// Will this code EVER execute?
[...]
}
Bob Powell [MVP]
2007-04-12 18:41:01 UTC
Permalink
Ahh, hmmm, in fact a string variable _can_ be null.
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
Post by Jeff Johnson
Subject pretty much says it all. I was wondering if it was EVER necessary
string myString = TextBox1.Text;
if (myString == null)
{
// Will this code EVER execute?
[...]
}
unknown
2009-09-30 07:57:53 UTC
Permalink
Yes you can set the value of a textbox.Text to be null, but upon retrieval, if the value is null, it will return back to you a String.Empty.

That is if you have not overriden the Text property of a Web.UI.TextBox. In case you really need to get back null for some reason or do whatever, you can extend the Textbox and provide your own implementation.





Bob Powell [MVP] wrote:

Re: Can TextBox.Text ever be null?
12-Apr-07

Ahh, hmmm, in fact a string variable _can_ be null

--
Bob Powell [MVP
Visual C#, System.Drawin

Ramuseco Limited .NET consultin
http://www.ramuseco.co

Find great Windows Forms articles in Windows Forms Tips and Trick
http://www.bobpowell.net/tipstricks.ht

Answer those GDI+ questions with the GDI+ FA
http://www.bobpowell.net/faqmain.ht

All new articles provide code in C# and VB.NET
Subscribe to the RSS feeds provided and never miss a new article



Jeff Johnson wrote:

EggHeadCafe - Software Developer Portal of Choice
WPF DataGrid Custom Paging and Sorting
http://www.eggheadcafe.com/tutorials/aspnet/8a2ea78b-f1e3-45b4-93ef-32b2d802ae17/wpf-datagrid-custom-pagin.aspx
Jeff Johnson
2009-10-01 13:08:30 UTC
Permalink
Post by unknown
Yes you can set the value of a textbox.Text to be null, but upon
retrieval, if the value is null, it will return back to you a
String.Empty.
That is if you have not overriden the Text property of a Web.UI.TextBox.
In case you really need to get back null for some reason or do whatever,
you can extend the Textbox and provide your own implementation.
This question was asked (by me) TWO AND A HALF YEARS AGO! Lately I've seen
TONS of this from places like Egghead Cafe. Please STOP responding to
ANCIENT posts. It's bad netiquette to do so.

Loading...