Multiple IF-statement conditions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • E11esar
    New Member
    • Nov 2008
    • 132

    Multiple IF-statement conditions

    Hi there.

    I need a sanity check here.

    I have an array of strings and am running the following block of code in C#:

    Code:
    string[] cols = line1.Split(separators.ToCharArray());
                   
    if  ((cols[6] == "1") && (cols[23] == "Y") && (!(cols[20] == "STR REC ")))
    {
      //Do something...
    }
    When I debug this, the respective watch values are:

    cols[6] = "1"
    cols[23] = "\"Y\""
    cols[20] = "\"RD REC\""

    So I would have expected this to enter the body of the IF-statement and execute the respective code, but that isn't happening.

    Hence can anybody see where my logic is going amiss please?

    Thank you.

    Mark :)
    Last edited by Curtis Rutland; Dec 11 '08, 02:54 PM. Reason: Added Code Tags - Please surround your code with [CODE] and [/CODE]
  • nukefusion
    Recognized Expert New Member
    • Mar 2008
    • 221

    #2
    The only thing I can see at the moment is that your watch values for cols[23] and cols[20] have some quotation mark string literals in them that you're not testing for in your conditional statement.

    Comment

    • E11esar
      New Member
      • Nov 2008
      • 132

      #3
      IF-Condition problems

      Hi again.

      I have tried amending the if-condition to look as follows:

      Code:
      if  ((cols[6] == "\"1\"") && (cols[23] == "\"Y\"") && (!(cols[20] == "\"STR REC \""))) 
      { 
        //Do something... 
      }
      But still no joy when all conditions are met. Sorry for being so daft but is there anything else I can do to get the logic to work here please?

      Thank you.

      M :)

      Comment

      • nukefusion
        Recognized Expert New Member
        • Mar 2008
        • 221

        #4
        Hi, no problem. What are the watch values when it's failing in this particular case?

        I see you've added quotation mark literals for the check against cols[6] as well, although in the watch value in your first post, col[6] doesn't contain quotation marks. Did you mean to do that?

        Even easier might be to just post exactly what you want the contents of cols 6, 20 and 23 to be in order for the condition to pass in a plain english sentence. I'll then help you to write the IF statement.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          One might consider doing this:

          [code=c#]
          bool a = (cols[6] == "1");
          bool b = (cols[23] == "Y");
          bool c = (cols[20] == "STR REC ");
          if ( a && b && (!c))
          {
          //Do something...
          }
          [/code]

          And then you can see which watch value failes to bring it into the loop.

          Also, for !(cols[20] == "STR REC "), why not just do this:
          (cols[20] != "STR REC ");

          Comment

          • E11esar
            New Member
            • Nov 2008
            • 132

            #6
            IF-Condition

            Hi there.

            No that is right with the cols[6] value being in quotes and the watch values are as stated.

            In effect I have a CSV file and hence this is where all the quotes are coming from but again, I think I'm getting myself into a tizz here.

            If I use a MessageBox to display the respective values then for a given example,

            cols[6] = 1, cols[23] = "Y" and cols[20] = "STR REC".

            Hopefully that clarifies matters..?

            Thank you for your help.

            M :)

            Comment

            • E11esar
              New Member
              • Nov 2008
              • 132

              #7
              Me again

              Hi there.

              I think I just solved it:

              I have used the following line:

              Code:
              if  ((cols[6] == "1") && (cols[23] == "\"Y\"") && (!(cols[20] == "\"STR REC \"")))  
              {  
                //Do something...  
              }
              and this is now behaving as required.

              Amazing how the simple things can throw you sometimes.

              Thank you again for your help; I'm sure I'll be back again soon as I'd be lost without this excellent forum!

              M :)

              Comment

              Working...