Loop not going inside when the value is same [closed]

Because you have a space in TEMP. " 74". The two values are not equal because the second one TEMP2 is "74" without the space.

Try using Trim on your values to remove whitespace:

string temp = ids[i].Trim();
string temp2 = raditem.Value.ToString().Trim();

Also, always use the .Equals overload for the String class, as you can control the culture for the string comparison. In this case it doesn’t matter because they’re numerals but it could matter if you want to compare to alphabetical strings where you want “A” to equal “a” and so forth.

Furthermore, if you are sure your array contains only numbers, I could recommend converting the values to numbers before comparing them.

// one way to validate is wrap this in a try-catch and handle input format error.
int temp = Convert.ToInt32(ids[i].Trim()); 

That way if you have a case where there aren’t numbers, you could validate and complain to the user or whatever you want.

Browse More Popular Posts

Leave a Comment