By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,615 Members | 1,961 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,615 IT Pros & Developers. It's quick & easy.

Multiple IF-statement conditions

100+
P: 132
Hi there.

I need a sanity check here.

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

Expand|Select|Wrap|Line Numbers
  1. string[] cols = line1.Split(separators.ToCharArray());
  2.  
  3. if  ((cols[6] == "1") && (cols[23] == "Y") && (!(cols[20] == "STR REC ")))
  4. {
  5.   //Do something...
  6. }
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 :)
Dec 11 '08 #1
Share this Question
Share on Google+
6 Replies


nukefusion
Expert 100+
P: 221
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.
Dec 11 '08 #2

100+
P: 132
Hi again.

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

Expand|Select|Wrap|Line Numbers
  1. if  ((cols[6] == "\"1\"") && (cols[23] == "\"Y\"") && (!(cols[20] == "\"STR REC \""))) 
  2.   //Do something... 
  3.  
  4.  
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 :)
Dec 11 '08 #3

nukefusion
Expert 100+
P: 221
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.
Dec 11 '08 #4

Plater
Expert 5K+
P: 7,872
One might consider doing this:

Expand|Select|Wrap|Line Numbers
  1. bool a = (cols[6] == "1");
  2. bool b = (cols[23] == "Y");
  3. bool c = (cols[20] == "STR REC ");
  4. if  ( a && b && (!c)) 
  5.   //Do something... 
  6.  
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 ");
Dec 11 '08 #5

100+
P: 132
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 :)
Dec 11 '08 #6

100+
P: 132
Hi there.

I think I just solved it:

I have used the following line:

Expand|Select|Wrap|Line Numbers
  1.  
  2. if  ((cols[6] == "1") && (cols[23] == "\"Y\"") && (!(cols[20] == "\"STR REC \"")))  
  3. {  
  4.   //Do something...  
  5. }  
  6.  
  7.  
  8.  
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 :)
Dec 11 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.