'c# textbox zero validation

I've been facing a very basic issue, I want to validate textbox not equals to zero( != "0" ) validation. But even though the textbox is zero the if condition I used to validate becomes true and allows.

textbox.text = "0";
if(textbox.text != "0")
{
//textbox value is zero but, if statement becomes true somehow and execute the code inside the if statement that shouldn't be happen right?. I need to know why.
}

But if I validate for equals to zero( == "0" ) works

if(textbox.text == "0")
{
//do something
}
else
{
//now the condition worked and comes to else part.
}

This also happens for validating null or empty, I know we can user string.IsnullorEmpty to validate string or null. But I want to know why even though the textbox value is null or empty or zero the if statement for not equals isn't working.



Solution 1:[1]

"the textbox is zero" You need to learn the difference between = and ==.

textbox.text == "0" checks if the value of the .text property is zero and returns true or false

textbox.text = 0 sets the value of the .text property to zero

Instead of:

    textbox.text=="0";
    if(textbox.text!="0")
    {
    //do something.
    }

Do:

    textbox.text="0";
    if(textbox.text!="0")
    {
    //do something.
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nikola Develops