Elfoc
Elfoc

Reputation: 3689

Null or "0" in text box in C#

i'd like to hide item (panel) when textbox value is 0 or null.

        if (Wier_r1.Text == null | Wier_r1.Text=="0") panelR1.Visible = false;

        else panelR1.Visible = true;

Why it doesn't work when in texbox is nothing - null. With 0 is working.

Upvotes: 3

Views: 5506

Answers (11)

Steve
Steve

Reputation: 216273

Because TextBox.Text is never null. Is an empty string.

Your test should be:

 if (Wier_r1.Text == string.Empty || Wier_r1.Text=="0") 
     panelR1.Visible = false; 
 else 
     panelR1.Visible = true; 

and the correct OR opertator in this case is || not |.

Upvotes: 5

Hamza Waqas
Hamza Waqas

Reputation: 629

Treat as object and use .equal for it..

    if (Wier_r1.Text.equals("") || Wier_r1.equals(0)) 
    panelR1.Visible = false;
else 
    panelR1.Visible = true;

Upvotes: 0

Eric
Eric

Reputation: 3334

I don't think a text box will ever return null for the Text value. I believe it returns an empty string.

You'd want to do something like this I think:

if( string.IsNullOrEmpty(Wier_r1.Text) || Wier_r1.Text == "0"){
  // Do something
}

Upvotes: 7

thmsn
thmsn

Reputation: 1996

I would probably do it like this

panelR1.Visible = !(string.IsNullOrEmpty(Wier_r1.Tex) || Wier_r1.Text=="0");

And as others have stated, the logical or operator in C# is ||, a single | is a bitwise or.

Upvotes: 0

punker76
punker76

Reputation: 14611

panelR1.Visible = !string.IsNullOrEmpty(Wier_r1.Text) && Wier_r1.Text != "0";

Upvotes: 4

M Granja
M Granja

Reputation: 865

I'm not sure the Text property of a textfield is ever null. In doubt, use this method:

if (System.String.IsNullOrEmpty(Wier_r1.Text) || Wier_r1.Text=="0") panelR1.Visible = false;

I'm assuming you want to hide the panel when there is no text, right? That would be an empty string.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062600

.Text on a TextBox is not going to be null. If you mean a blank string, then test against "", or use:

if(string.IsNullOrEmpty(Wier_r1.Text) || Wier_r1.Text == "0") {...}

Note also the difference between | and || which changes it to a short-circuiting "or" (i.e. if the first is true it won't evaluate the second) - which is usually prefreable, but which shouldn't change the logic in this particular scenario,

Upvotes: 4

vc 74
vc 74

Reputation: 38179

Try (|| operator)

if (string.IsNullOrEmpty(Wier_r1.Text) || Wier_r1.Text=="0")

Or short form:

panelR1.Visible = (!string.IsNullOrEmpty(Wier_r1.Text)) && (Wier_r1.Text != "0");

Upvotes: 1

Josh Mein
Josh Mein

Reputation: 28625

Textboxes dont have nulls stored in them; they are always a string so you need to check for "".

if (Wier_r1.Text == "" || Wier_r1.Text=="0") 
    panelR1.Visible = false;
else 
    panelR1.Visible = true;

Upvotes: 1

Ta01
Ta01

Reputation: 31610

try (Wier_r1.Text == null || Wier_r1.Text == "0")

Upvotes: 0

4b0
4b0

Reputation: 22323

try:You are missing to use or || properly.

if (Wier_r1.Text == null || Wier_r1.Text=="0") panelR1.Visible = false;
else panelR1.Visible = true;

Upvotes: 1

Related Questions