Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C sharp logic question

Posted on 2010-11-16
12
Medium Priority
?
772 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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 2000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

782 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