Kevin
Kevin

Reputation: 403

Problems with OR operator

I am using a foreach loop which repeats itself based on the amount of items in a list. Each time it repeats, it checks if two textboxes have a value of "0". If they (or one of them) does, then I check if the summary value of the hour it is iterating on is (or is not. in the case of the part after the "end" comment) a certain value. If it isn't, then I used an OR operator (||) to do the same thing to the next one. However, this is not working.

I am using debug.writeline to see where the loop finds a match. It is, for whatever reason, always at 0 - even if the value of hour.summary for iteration 0 is "Rain", something which should make the if not be able to find a match and instead fire the else.

foreach (var hour in result.Hourly.Hours)
{
    //Start
    if (starttemp.Text == "0")
    {
        if ((hour.Summary == "Rain") || (hour.Summary == "Snow") || (hour.Summary == "Drizzle") || (hour.Summary == "Light Rain") || (hour.Summary == "Light Snow") || (hour.Summary == "Heavy Rain") || (hour.Summary == "Heavy Snow"))
        { starttemp.Text = hour.Temperature.ToString(); Debug.WriteLine("SET START WITH " + hour.Summary + " AT HOUR " + increment); }
        else
        { }
    }
    //End
    if (endtemp.Text == "0")
    {
        if ((hour.Summary != "Rain") || (hour.Summary != "Snow") || (hour.Summary != "Drizzle") || (hour.Summary != "Light Rain") || (hour.Summary != "Light Snow") || (hour.Summary != "Heavy Rain") || (hour.Summary != "Heavy Snow"))
        { endtemp.Text = hour.Temperature.ToString(); Debug.WriteLine("SET END WITH " + hour.Summary + " AT HOUR " + increment); }
        else
        { }
    }
    increment = increment + 1;
}

Upvotes: 0

Views: 38

Answers (1)

Jakob Olsen
Jakob Olsen

Reputation: 823

The line

if ((hour.Summary != "Rain") || (hour.Summary != "Snow") || (hour.Summary != "Drizzle") || (hour.Summary != "Light Rain") || (hour.Summary != "Light Snow") || (hour.Summary != "Heavy Rain") || (hour.Summary != "Heavy Snow"))

Will always be true...

I am not 100% sure what you are trying to do, but i think you should replace the || with &&

Upvotes: 2

Related Questions