简体   繁体   中英

Process data and continue if NULL

I have to process a number of items of data for example

User u = NotMyObject.GetUser(100);
ProcessProperty(u.FirstName);
ProcessProperty(u.Surname);
ProcessProperty(u.Phone.Work);
ProcessProperty(u.Phone.Mobile);
...
ProcessProperty(u.Address.PostCode);

Take it that all properties are returned from GetUser(...) as string. What ProcessProperty does is, I hope, not relevant (maybe write the value to a file, for example) but it would look like:

private void ProcessProperty(string data) {
...
}

My question is given that u.Phone & likewise u.Address may be NULL how can I process the "User u" object without putting each ProcessProperty(...) call in a try/catch block?

Apologies if the formatting of the question is no good, I'm still getting the hang of posting.

Many thanks. N.

You could try (maybe it's not elegant):

ProcessProperty(u.Phone == null ? null : u.Phone.Work);

and

private void ProcessProperty(string data) {
    if (String.IsNullOrEmpty(data)) {
        ....
    } else {
        ....
    }
}

如果您可以编辑User类,我将更改属性上的Set代码,该属性可以为null,以将NULL转换为String.Empty

if you have control of the ProcessProperty function then you can just add this inside the function

if(data == null) return;

otherwise you can just write this

if(u.Phone != null)
     ProcessProperty(u.Phone.Work);

Use lambda expressions and an extension method to make a maybe monad:

public static class ObjectExt
{
    public static TProp GetPropOrNull<TObj, TProp>(this TObj obj, 
        Func<User,TProp> getProp)
        where TObj : class
        where TProp : class
    {
        if (obj == null)
            return null;
        else
            return getProp(obj);
    }
}

Usage:

u.GetPropOrNull(obj => obj.Phone).GetPropOrNull(obj => obj.Work);
u.GetPropOrNull(obj => obj.Phone).GetPropOrNull(obj => obj.Home);

Instead of using a long dot chain (like u.Phone.Work.Whatever) which fails if any of the first properties are null, this method short-circuits and returns null as soon as it sees a null. This can shorten your code by a great deal (as opposed to using if-checks) if you have to work with a property deep down in someone else's class who maybe isn't a very careful programmer. I think in the article below, the author uses the method name With instead of GetPropOrNull , so that even shortens it up further.

Reference:

If you CAN'T change ProcessProperty consider wrapping it:

private void ProcessPropertySafe(string data) 
{
    if (data != null)
    {
        ProcessProperty(data);
    }
}

or do it like int.Parse() vs. int.TryParse():

private bool TryProcessProperty(string data) 
{
    try
    {
        ProcessProperty(data);
        return true;
    }
    catch // <- That's not clean ...
    {
        return false;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM