Solved

C sharp logic question

Posted on 2010-11-16
12
755 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

757 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

19 Experts available now in Live!

Get 1:1 Help Now