user2509901
user2509901

Reputation:

What is the difference between ("") and (null)

While trying to set Validations i initially encountered some problems with checking if a textbox is null, i tried using

    private void btnGo_Click(object sender, EventArgs e)
    {
        string name = textLogin.Text;
        if (name == null)
        {
             labelError.Visiblle = true;
             labelError.Text = "Field Cannot be Left Blank"
        }
    }

but it didn't work, until i tried this

    private void btnGo_Click(object sender, EventArgs e)
    {
        string name = textLogin.Text;
        if (name == "")
        {
             labelError.Visiblle = true;
             labelError.Text = "Field Cannot be Left Blank"
        }
    }

My question is i want to know the difference between ("") and (null) and why null wasn't working.

Thanks in advance

Upvotes: 13

Views: 14566

Answers (10)

Sibulele
Sibulele

Reputation: 390

Firstly, Null and Empty String i.e " " are not the same. Null is an object reference value. Null means no value while " " implies that there is a value, the value is an empty character.

An Object initialized to Null means that in memory this Object is not pointing to anything. It has got no worth or value. It's just declared. This doesn't mean it's value is "" or Zero.

While an Object initialized to " " means that in memory this Object is pointing to the byte code of " ". So this Object has a value.

In the context of a TextField, the TextField accepts characters, i.e letters, punctuations and numbers. A TextField does operations on text not Null.

If you were to say

    String name = textLogin;
    if (name == null){
        }

( maybe you would need to cast textLogin to String, I'm not sure)

This would work because textLogin is an object. It would return the results that you want because this Object can have a null reference value.

But it would mean if you want the value of this textLogin, inside your "if" statement you would have to say, name = textLogin.Text. This would give you a content inside the textLogin Object.

Since you chose to use

  String name = textLogin.Text;
  if (name == null){
        }

It's as if textLogin.Text is already reference to an empty string should the TextField be empty. This is done under the hood for you so you don't have to deal with a NullPointExceptionError.

So "name" can never be null. "name" can either be an empty string or a value.

Upvotes: 0

Ry-
Ry-

Reputation: 224886

The same as the difference between 0 and an empty array: everything. They’re different values. "" is an empty string, and that’s what a blank textbox holds as text is all. null is no value, and is not what a blank textbox has as Text.

Upvotes: 21

tia
tia

Reputation: 9698

In layman's term, null means lacking of value and "" mean zero-length string, which is not the same thing. There might be some piece of software that treat null string and "" equally e.g. Console.WriteLine, but it still does not make them the same thing.

Strictly speaking, "" == null expression is false by design. The equality comparison of String type in .NET framework works by == operator overloading, which does not treat null as equal to "".

Upvotes: 2

user2600396
user2600396

Reputation:

The difference is that "" means and empty string but null means it doesn't exist

Upvotes: 1

Mayank
Mayank

Reputation: 8852

Simple, "" has a valid value i.e. String.Empty but null doesn't have any value.

Upvotes: 1

p.s.w.g
p.s.w.g

Reputation: 149010

The System.String data type in .NET is a class, a reference type. So an empty string ("" or string.Empty) is a reference to a value with zero length, while null does not reference a to real value, so any attempt to access the value it references will fail.

For example:

string emptyString = "";
string nullString = null;

Console.WriteLine(emptyString.Length); // 0
Console.WriteLine(nullString.Length);  // Exception!

I'd recommend you use IsNullOrEmpty (or IsNullOrWhiteSpace) in your validation code, to handle both cases:

if (string.IsNullOrEmpty(name))
{
     labelError.Visiblle = true;
     labelError.Text = "Field Cannot be Left Blank"
}

Upvotes: 6

King King
King King

Reputation: 63317

The default value of TextBox.Text is String.Empty or "" not null. So your first code didn't work. null is just to indicate that an object doesn't point to anything, it's not allocated with any memory.

Upvotes: 3

edtheprogrammerguy
edtheprogrammerguy

Reputation: 6039

You can use IsNullOrWhiteSpace to do text box input validation. It checks for null, empty string or white space (tab, space, etc.). http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace.aspx

Upvotes: 1

J Max
J Max

Reputation: 2381

"" is an empty string vs null which means "does not exist".

In your case, you first compared name to "does not exist" which was false because name did exist. Then you compared name to empty string which is true because it has the value of an empty string.

Upvotes: 5

miguel.martin
miguel.martin

Reputation: 1627

null simply means that the object (in this case, the textLogin.Text object) does not exist. In order for this to be the case, the textLogin object cannot exist. Thus the textLogin object is not null in this case, and thus textLogin.Text cannot be null.

"" on the other hand means an empty string, meaning that there is nothing in the textbox's text. i.e. textLogin.Text does not contain any characters within it.

Upvotes: 2

Related Questions