Solved

C sharp logic question

Posted on 2010-11-16
12
757 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:ptemo
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 10

Expert Comment

by:Mez4343
ID: 34150450
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;
       
    }

0
 
LVL 13

Expert Comment

by:AngryBinary
ID: 34150522
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.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34150528
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
0
 
LVL 13

Expert Comment

by:AngryBinary
ID: 34150532
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.
0
 

Author Comment

by:ptemo
ID: 34150919
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
0
 
LVL 3

Expert Comment

by:JamesAperta
ID: 34151069

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

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:JamesAperta
ID: 34151179
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

0
 

Author Comment

by:ptemo
ID: 34151196
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
0
 
LVL 3

Expert Comment

by:JamesAperta
ID: 34151231
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

0
 
LVL 3

Accepted Solution

by:
JamesAperta earned 500 total points
ID: 34151251
check my latest comments indicating that you need to convert the Value to an integer (because it's a string when it comes in).   also use a try catch block to handle any invalid input.   should add a catch for any exception really using    catch (Exception E) in case it's not a format exception getting thrown
0
 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 34152010
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;
0
 
LVL 13

Expert Comment

by:AngryBinary
ID: 34152210
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# Offline Apllication 5 56
Turn on intranet settings 1 42
Help with C#, MVC, razor. 6 23
Video Player 2017 5 17
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now