Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tobias M. Madsen 12 posts 102 karma points
    Jan 29, 2020 @ 08:11
    Tobias M. Madsen
    0

    HasValue vs Strongly Typed

    Straight to the point, was is the advantage of using HasValue("something") vs strongly typed? Is it cache related, or just a simpler way of checking values?

    string awesomevar = Model.Content.GetPropertyValue<string>("somestring");
    
    // This
    if ( !awesomevar.IsNullOrWhiteSpace() ) {}
    
    // VS this
    if ( Model.Content.HasValue("somestring") ) {}
    
  • Steve Megson 151 posts 1022 karma points MVP c-trib
    Jan 29, 2020 @ 10:17
    Steve Megson
    2

    They should be pretty much equivalent for string values. HasValue does a simpler test, so it may be slightly quicker if you're filtering a large list of content.

    For other strongly-typed values, reading the value may be a stronger test. HasValue always just tests whether there's a non-empty, non-whitespace value for that property in the XML cache. GetPropertyValue<T> also checks that it can convert that value to the type you asked for. Perhaps you've got a content picker which refers to an item which has been deleted. HasValue will just see the ID and return true, but GetPropertyValue<IPublishedContent> would return null.

  • Tobias M. Madsen 12 posts 102 karma points
    Jan 29, 2020 @ 11:41
    Tobias M. Madsen
    0

    So the strongly typed / native way of null checks and so on, would be preferable for more correct results?

    And in theory HasValue should be faster after the content has been cached? But can give incorrect results in case of cache issue?

Please Sign in or register to post replies

Write your reply to:

Draft