How did the code execution get inside this 'if' statement?

188 views Asked by At

Look at the following code:

    public static string GetCleanURL(string baseURL, string url)
    {
        string cleanUrl = url.ToLower();
        if (cleanUrl.StartsWith("http://"))
        {//It already starts with http://  It is already in the correct form return it.
            return cleanUrl;
        }

The 'url' value passed in is "123.123.123.123:1234/myurl/withstuff.xml". In the 'if' statement, the value for 'cleanUrl' is "123.123.123.123:1234/myurl/withstuff.xml". But for some reason, the code execution goes inside of the if block and 'return cleanUrl;' gets executed.

Here is a screenshot of the current value for 'cleanUrl':

enter image description here

When I plug cleanUrl.StartsWith("http://") into the 'Immediate Window' of my debugger, it returns false. Which is what I would expect. However, the execution is somehow going into the if block as though it had returned true.

Can anybody please explain how this is possible?

2

There are 2 answers

2
Curtis On BEST ANSWER

SOLVED !!!

I appreciate those of you who helped me out on this one.

I needed to Clean and Rebuild my project and Close and Reopen Visual Studio 2013 about 4 times before the code base and debug stuff was actually in sync. It now appears to be working correctly.

Not sure why it ever got that way, or why I needed to do Clean/Rebuild several times before things synced up. But it is working now.

So, friends, if ever you find your code is just acting crazy and not doing what it should do. Just realize that anybody in their right mind would never become a programmer. Then do a clean / rebuild a few times and pray that the oddity goes away never to return.

Thanks for all your help on this one.

I LOVE fighting with the development tools...

8
Binary Worrier On

No, StartWith isn't buggy, and works as expected.

Try the minimal code below. Reduce your code to the minimum amount to reprouce the problem. Does this happen everytime? Does it happen with more than one candidate string? Or only that string?

Something else is going on sorry, the following writes Doesn't start

    static void Main(string[] args)
    {
        string cleanUrl = "123.123.123.123:1234/SomeFile.xml";
        if (cleanUrl.StartsWith("http://"))
            Console.WriteLine("Starts");
        else
            Console.WriteLine("Doesn't start");
        Console.ReadLine();
    }