locked
If statement does not get satisfied RRS feed

  • Question

  • string sTicket = null;
    string sProduct = null;
    foreach (Iincident s in HostRTU.OpenIncidents)
    {
       sTicket = s.ID;
       sProduct = GetName(s.ToString());
       if (sProduct == "XYZ") Console.WriteLine("{0}	{1}", sTicket, sProduct);//..........> this line does not get printed!
       break;
    }
    Console.WriteLine("The product is{0}	", sProduct);
    


    With the above snippet of code, the if statement does not get satisfied somehow. why?

    My aim is to put the break statement within it so when sProduct is XYZ, the loop ends.

    Thanks,

    Sunday, September 11, 2011 11:09 PM

Answers

  • Change your debug statment so the WriteLine is as this:
     
        Console.WriteLine ("[{0}] and [{1}]", sTicket, sProduct);


     

    --
    Mike
    • Marked as answer by bocais Monday, September 12, 2011 12:20 AM
    Sunday, September 11, 2011 11:59 PM
  • Try the following.

    if(sProduct.Trim() == "XYZ")

    {

       Console.WriteLine("{0}  {1}", sTicket, sProduct);

       break;

    }

     

    If it works, it's because sProduct has leading or trailing spaces.


    • Marked as answer by bocais Monday, September 12, 2011 12:20 AM
    Monday, September 12, 2011 12:06 AM

All replies

  • As it's written, the break will be executed the first time through the loop, so unless the first product is XYZ, the line won't be printed.

    Try...

     

    if(sProduct == "XYZ")

    {

       Console.WriteLine("{0}  {1}", sTicket, sProduct);

       break;

    }

     

    ... so that it only breaks when the if is true.

    • Proposed as answer by Matthew Watson Sunday, September 11, 2011 11:19 PM
    Sunday, September 11, 2011 11:19 PM
  • I tried that first but it did not break.
    Sunday, September 11, 2011 11:28 PM
  • Okay, you've lost me.

     

    Are you saying that it printed the line that you're looking for, but then kept executing the loop? If so, how do you know that it kept executing the loop?

    Or are you saying that the line didn't print, and the loop kept getting executed? If this is the case, you must not have a product called "XYZ". Remember, the == test is case-sensitive and will look at trailing spaces or any other funny characters that may have found their way into the string.

    Sunday, September 11, 2011 11:38 PM
  • I know I have XYZ (capitalised) because when I don't put the if statement altogether and do not want to break out of the foreeach loop, I get sTicket and sProduct printed for each item. I get "1234     XYZ" printed out.

    I looked carefully, perhaps I typed XYZ wronly, but did not find any spelling mistake. However I copied the printed XYZ from before I added the if and break statement and pasted it into the if statement, but it did not work!!



    • Edited by bocais Sunday, September 11, 2011 11:53 PM
    Sunday, September 11, 2011 11:52 PM
  • Change your debug statment so the WriteLine is as this:
     
        Console.WriteLine ("[{0}] and [{1}]", sTicket, sProduct);


     

    --
    Mike
    • Marked as answer by bocais Monday, September 12, 2011 12:20 AM
    Sunday, September 11, 2011 11:59 PM
  • Try the following.

    if(sProduct.Trim() == "XYZ")

    {

       Console.WriteLine("{0}  {1}", sTicket, sProduct);

       break;

    }

     

    If it works, it's because sProduct has leading or trailing spaces.


    • Marked as answer by bocais Monday, September 12, 2011 12:20 AM
    Monday, September 12, 2011 12:06 AM
  • Hi Family and Ante.

    There was a little space preceding the name.

    Thank you very much.

    Monday, September 12, 2011 12:21 AM