Solved

C sharp logic question

Posted on 2010-11-16
12
767 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

734 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