Potential Bug with C#

The screenshots speak for themselves. Debug - 4 is the most relevant shot.

Take a look at the values shown in Watch 1, and the execution path that is being followed. What should happen is, base.Text == null evaluates to true, null is returned and life is good.

What does happen, is that some how it passes over that part of the if, falls into the else and then executes the code highlighted in Debug - 4. A null reference exception is then thrown, because I try to call base.Text.Trim() when base.Text is null. Even though it should NEVER have gotten to that point.

I hope the problem is clear enough. I'm running VS2005 RTM - I've just been to busy to move over to the release version.

Screenshots are here, they appear to not be displaying correctly
http://img286.imageshack.us/img286/4056/13ct1.png
http://img145.imageshack.us/img145/9246/27qz.png
http://img182.imageshack.us/img182/3786/31uj.png
http://img145.imageshack.us/img145/8637/49al.png






Answer this question

Potential Bug with C#

  • EmmaTaylor01

    From what I see, this could only happen if calling the base class's Text property changes the value returned from it. You should try and use reflector to check out the code of the property and whatever it is that it calls.
    Maybe you could use the new tool called Deblector (a Debugger using Reflector) to step through the code: http://www.felicepollano.com/PermaLink,guid,1c863e69-1b56-4cfa-a60b-203e1127b8bd.aspx

    I'd love to know what happens when you use it with the latest version.

    Best of luck.


  • Karim

    Perhaps the string is empty (text=="") rather than null

    Anyhow, with a try {...} catch {return null} you can solve the problem.


  • TroyB

    Try assigning the value to a local variable and then test the local variable.

    Please let me know what happens then - this is really interesting :D


  • jcdevnet

    In .NET v2.0.50727 System.Web.UI.WebControls.TextBox.Text will never return null.

    I can't explain what you're seeing though when debugging with your framework version.


  • LAURA PLATA

    Hmm, try putting a "lock(this){...}" around the entire body of the "get", and see if it still happens.

  • Manikandan S

    As an aside, returning null from the Text property appears to break some other things that happen in the base class, so I have abandoned this idea. It is still a strange situation though.

  • e-mhigz

    http://img156.imageshack.us/img156/1524/51wl.png

    Thanks for the suggestion - the result was pretty unexpected. I declared the string and assigned null to it, just in case. I then assigned base.Text to it - check out the watch in the screenshot. The string I assign to is suddenly instantiated as an empty string, but base.Text is still null.

    Not particularly useful, definitely but interesting :)

    It's any easy enough problem to work around, but I'd love to know why it's happening. I've got a virtual pc with the VS2005 release version (rather than the RTM), so I'll move my code over to that later today and test it some more.

  • Potential Bug with C#