Copied to clipboard

Flag this post as spam?

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


  • Robzor 8 posts 72 karma points
    Jul 24, 2015 @ 15:36
    Robzor
    0

    Testing if current Member has a Property

    I'm currently getting Member values like so:

    variable = "PropertyName";

    value = member.GetValue(variable).ToString();

    I get an error if member.GetValue(variable) doesn't exist however, so I'm trying to test for that, by saying if member.GetValue(variable) is null then set "value" to something static. Unfortunately as soon as member.GetValue(variable) is reached it just throws a "given key was not present in the dictionary" error if it doesn't exist.

    Any idea how I can test for this?

    Many thanks.

  • Jamie Pollock 174 posts 853 karma points c-trib
    Jul 24, 2015 @ 15:52
    Jamie Pollock
    100

    Hey Robzor,
    You could try the following bit of code:

    member.HasProperty(variable) && member.GetValue(variable) != null;
    

    Or possibly turn it into an Extension Method for uses with other property aliases.

    public static bool HasPropertyValue(this IMember member, string propertyAlias) {
        return member.HasProperty(propertyAlias) && member.GetValue(propertyAlias) != null;
    }
    

    I hope this helps.

    Thanks,
    Jamie

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jul 24, 2015 @ 15:57
    Sebastiaan Janssen
    1

    Or, if your member is of type IPublishedContent (I don't know which member API you're using) then it's as simple as: member.HasValue(variable).

  • Robzor 8 posts 72 karma points
    Jul 28, 2015 @ 08:17
    Robzor
    0

    Hi Jamie,

    member.HasProperty(variable) && member.GetValue(variable) != null;
    

    This worked perfectly, thanks a lot!

Please Sign in or register to post replies

Write your reply to:

Draft