Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
2.60/5 (5 votes)
See more:
Hi All,
I m trying to execute following code in C# .Net
C#
private void txtRollNumber_KeyDown(object sender, KeyEventArgs e)
{
    label1.Enabled = true;
    bool result = false;
    if (txtRollNumber.Text == false)                    
    {                    
        label1.Text="Please Enter value in Roll Number";
        txtRollNumber.Select();
    }
    else
    {
        txtname.Select();
    }
}       

but it shows an error in this line (txtRollNumber.Text == false) it shows that

Operator '==' cannot be applied to operands of type 'string' and 'bool'
Please help what should I do ?

Thanks & Regards
Indrajit
Posted
Updated 11-Apr-11 0:29am
v2
Comments
Kim Togo 11-Apr-11 7:01am    
What type of class is "txtRollNumber" ?, TextBox control etc.

You can compare strings with booleans like you may do with apples and computers ( :rolleyes: ).
To fix the issue compare an expression with another of the same type, e.g.

// string to string comparison
if ( txtRollNumberTxt.Text == String.Empty )


or

// bool to bool comparison
if ( String.IsNullOrEmpty(txtRollNumberTxt.Text) == true )


Please note the above code maybe expressed more concisely with
if ( String.IsNullOrEmpty(txtRollNumberTxt.Text) )
 
Share this answer
 
v6
Comments
Kim Togo 11-Apr-11 6:58am    
IsNullOrEmpty is a static function under String class. "txtRollNumberTxt.Text.IsNullOrEmpty()" is not possible out of the box. Via extension method under .NET, then yes you can make something like this.
CPallini 11-Apr-11 8:05am    
You are right, of course. I have (hopefully) fixed my answer (SAKryukov would say the mistake was caused by my C++ imprinting). Thank you.
Kim Togo 11-Apr-11 8:06am    
C++ imprinting.. Hehe :-)
Sergey Alexandrovich Kryukov 11-Apr-11 13:14pm    
The comparison with Empty is good (with "" is not), but it won't compile: string.Empty is a read-only static property, not a function. Please fix by removing ().
--SA
CPallini 11-Apr-11 14:10pm    
Oh my God! My C# is definitely rusty. Thank you and my apologies to the OP.
Try this.

C#
private void txtRollNumber_KeyDown(object sender, KeyEventArgs e)
{
    label1.Enabled = true;
    bool result = false;
    if (string.IsNullOrWhiteSpace(txtRollNumber.Text))
    {
        label1.Text="Please Enter value in Roll Number";
        txtRollNumber.Select();
    }
    else
    {
        txtname.Select();
    }
}


One more thing.

String.IsNullOrWhiteSpace vs String.IsNullOrEmpty.

The code behind String.IsNullOrWhiteSpace is described like this:

return String.IsNullOrEmpty(value) || value.Trim().Length == 0;
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 11-Apr-11 13:12pm    
Correct, my 5.
--SA
Kim Togo 11-Apr-11 16:25pm    
Thanks SA :-)
RaviRanjanKr 12-Apr-11 0:19am    
Good Solution! have a my 5 :)
SIMPLEST...
if ( txtRollNumberTxt.Text.Trim() == String.Empty )
{
    label1.Text="Please Enter value in Roll Number";
    txtRollNumber.Select();
}
.
.
.
 
Share this answer
 
go and check the datatype that you are used it
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900