MVCToolkit and nulls

Last post 12-19-2007 1:34 PM by abombss. 1 replies.

Sort Posts:

  • MVCToolkit and nulls

    12-19-2007, 8:44 AM
    • Loading...
    • tgmdbm
    • Joined on 12-17-2007, 2:08 PM
    • Posts 639

    Hi

    I'd like the MVCToolkit to be tolerant of nulls. I've been using ?? "" to avoid the null errors.

    <%= Html.TextBox( "Name", ViewData.Name ?? "" ) %> 

     

    As an aside, I'm really excited about extension methods, cos you can call them on null objects!!!!!!

    public static string MyToString(this object obj)
    {
        return obj == null ? String.Empty : obj.ToString();
    }

    // then you can do this;

    object a = null;

    a.MyToString(); // This looks like you'll get a NullReferenceException

    But you don't! No errors, It returns the empty string if a is null. Something like this should be baked into the framework if you ask me.
     

  • Re: MVCToolkit and nulls

    12-19-2007, 1:34 PM
    • Loading...
    • abombss
    • Joined on 06-27-2006, 12:13 PM
    • Chicago, IL
    • Posts 164

     +1, NRE's in views are a pain.

    Adam Tybor -- abombss.com
    Filed under: , ,
Page 1 of 1 (2 items)
Microsoft Communities
Page view counter