C# Strings - Why does null give me a different result as opposed to ""?

Go To StackoverFlow.com

2

I have 2 forms set up. In the first form I have the following code.

frm_BL addBranch = new frm_BL();

do
{
    addBranch.ShowDialog();
    if (addBranch.txtAmount.Text == "")
    {
        break;
    }

} while (true);

In the main form. And just this in the second form.

private void btnAccept_Click(object sender, EventArgs e)
{
    this.Close();
}

However I found that if I change the code of the main form to:

 if (addBranch.txtAmount.Text == null) //changed to null

The second form keeps popping up. But if it stays at

if (addBranch.txtAmount.Text == "") 

It closes the form. Can someone explain why that is?

2012-04-04 04:40
by Pztar


5

The best way to do this is:

if (String.IsNullOrEmpty(addBranch.txtAmount.Text))

The txtAmount.Text property is a string containing the content of the textbox. If the textbox is empty then it's a zero-length string.

Checking for equality with null is saying "If the textbox doesn't have a string ...", which will always be false. The correct condition to check is "If the textbox's string is empty ...".

Using the IsNullOrEmpty method checks for both conditions. In this case the string should never be null, but it doesn't hurt to check.

Note that "" is an empty string (equivalent to String.Empty), whereas null says the string doesn't exist.

2012-04-04 04:47
by Andrew Cooper
Wow, that's a great answer. Thank - Pztar 2012-04-04 04:56


5

null and empty string are two different things, if you want to handle both cases you can use String.IsNullOrEmpty instead

null means the reference to the string you have does not exist (you point to nothing)

empty string means you have a reference to a string that contains nothing (pointer to an empty array of characters for example).

2012-04-04 04:43
by Jesus Ramos


2

A null String is different than an empty String. Use String.Empty() instead.

2012-04-04 04:42
by Ceramic Pot


0

I'd look at the definitions for more info:

The null keyword is a literal that represents a null reference, one that does not refer to any object. null is the default value of reference-type variables. Ordinary value types cannot be null. However, C# 2.0 introduced nullable value types.

http://msdn.microsoft.com/en-us/library/edakx9da.aspx

The value of this field (String.Empty) is the zero-length string, "". In application code, this field is most commonly used in assignments to initialize a string variable to an empty string.

http://msdn.microsoft.com/en-us/library/system.string.empty.aspx

One more thing that we see there is that "to test whether the value of a string is either Nothing or String.Empty, use the IsNullOrEmpty method."

So when something is null it represents a reference to nothing (most commonly it references 0), whereas when a string contains a null value this means that the string is empty but it holds a reference to valid memory.

2012-04-04 04:49
by Kiril


0

txtAmount.Text will never return a null whatsoever you write a code(in c# and for every valid (non null)TextBox).

TextBox.Text Returns a string which is either Empty string or non empty string.

2012-04-04 06:18
by Dhananjay


0

In addition to the already correct answers I would also add Trim() to the check because in most of the cases a string only with blanks is not something that is accepted especially for a TextBox input.

if (string.IsNullOrEmpty(addBranch.txtAmount.Text.Trim())) 
2012-04-04 06:47
by Dummy01
Ads