Link to home
Start Free TrialLog in
Avatar of ptemo
ptemo

asked on

C sharp logic question

Hello

Really new to C #, all I want to do is a simple if and statement, but I just cant figure it out.  Can someone please tell me what I'm doing wrong.  I'm wriiting a c sharp statement in asp.net environment.  

Here is the code, but It doesnt work, I keep getting an error message that says: Operator '&&' cannot be applied to operands of type 'bool' and string'.  


 void valComments_ServerValidate(Object source, ServerValidateEventArgs args)
    {
        if (args.Value.Length <= 30 && ins.Text = 1)
            args.IsValid = false;
        else
            args.IsValid = true;
       
    }


Thanks in advance

Avatar of Mez4343
Mez4343

Have to put quotes around the value you plan to check the .Text property:

 void valComments_ServerValidate(Object source, ServerValidateEventArgs args)
    {
        if (args.Value.Length <= 30 && ins.Text = "1")
            args.IsValid = false;
        else
            args.IsValid = true;
       
    }

The problem is you are using an assignment (=) instead of equivalence (==) operator in your if statement conditions. Use the following:

 void valComments_ServerValidate(Object source, ServerValidateEventArgs args)
    {
        if (args.Value.Length <= 30 && ins.Text == "1")
            args.IsValid = false;
        else
            args.IsValid = true;
       
    }

You can condense the statement, by the way, as follows:

 void valComments_ServerValidate(Object source, ServerValidateEventArgs args)
    {
            args.IsValid = (args.Value.Length <= 30 && ins.Text == "1");        
    }

... since the expression inside the parentheses returns a boolean value.
Avatar of Carl Tawn
Classic case of typing = instead of ==

Should say:

 void valComments_ServerValidate(Object source, ServerValidateEventArgs args)
    {
        if (args.Value.Length <= 30 && ins.Text == "1")
            args.IsValid = false;
        else
            args.IsValid = true;
        
    }

Open in new window


= is assignment
== is comparison
Correction... the condensed statement would be this:

    args.IsValid = !(args.Value.Length <= 30 && ins.Text == "1");

I forgot to put the "!" to switch the value of the boolean expression - IsValid should be false if the statement inside the parentheses is true, and vice versa.
Avatar of ptemo

ASKER

Thank you for the quick replies everyone.  

I actually meant to put this code

 if (args.Value <= 30 && ins.Text == "1")
            args.IsValid = false;
        else
            args.IsValid = true;


Essentially, it is the same code but without .length after value.  I want to know if a value is under 30, and if ins.text == "1".  But it won't work.  

Thanks again guys

if ((args.Value <= 30) && (String.Compare (ins.Text, "1", true) == 0))
    args.IsValid = false;
else
    args.IsValid = true;

Open in new window

Looks like ServerValidateEventArgs.Value is a string.  So need to convert to an integer before comparing against 30
if ((Convert.ToInt32 (args.Value) <= 30) && (String.Compare (ins.Text, "1", true) == 0))
    args.IsValid = false;
else
    args.IsValid = true;

Open in new window

Avatar of ptemo

ASKER

Hello James

Thank you for writing back.  I tried your code, but I got a red line under "args.Value <= 30", saying " Operator '<=' cannot be applied to operands of type 'string' and 'int'.

thanks again
might also want to add a try.. catch block around the check in case the Value supplied is not numeric.  You would want to catch a FormatException as follows
try
{
    {if ((Convert.ToInt32 (args.Value) <= 30) && (String.Compare  (ins.Text, "1", true) == 0))
        args.IsValid = false;
    else
        args.IsValid = true;
}
catch (FormatException vFE)
{
    args.IsValid = false;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of JamesAperta
JamesAperta

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Expanding on JamesAperta's answer, maybe ins.Text also needs conversion:

if ((Convert.ToInt32 (args.Value) <= 30) && (Convert.ToInt32 (ins.Text) == 1))
   args.IsValid = false;
else
   args.IsValid = true;
I wouldn't bother converting a text field to an integer if you just need to compare it to a literal value. Especially not without validating that it actually can be converted to a number first... And that's just extra lines of code messing up the place.