Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid null reference using c# possibilities

I'm trying to clean up a big handler method that show or not a the label on view.

The actual structure is something like:

if (Moo.Foo != null) {
    Show(TrType, LabelType, Moo.Foo.DangerousNullRef + " - " + Moo.Foo.AnotherPossibleNullRef);
}
else {
    DontShowField(TrType);
}

I'm thinking in something like send all components involved to a method that do all boring stuff, but:

ShowHandlingNull(Moo.Foo != null, TrType, LabelType, Moo.Foo.DangerousNullRef + " - " + Moo.Foo.AnotherPossibleNullRef);

Will cause null reference if Moo.Foo is null. Can I delegate or put in some action the behavior and put just one line in my big method?

like image 652
Custodio Avatar asked Mar 17 '26 21:03

Custodio


2 Answers

There's already the idea of using Func to handle this which seems to me like the best solution, I just made some assumptions on your intention and assume you are trying to get that label text so I wrote it up as such.

   private void YourFunction
    {
        Type TrType = this.GetType();
        MooClass Moo = new MooClass();
        LabelTypeEnum LabelType = LabelTypeEnum.something;
        ShowIf(Moo, TrType, LabelType, new Object[] { Moo.Foo, Moo.Foo2, Moo.Foo3 }, a => a.Foo.DangerousNullRef + " - " + a.Foo.AnotherPossibleNullRef);

    }


    void ShowIf(MooClass Moo, Type t, LabelTypeEnum LabelType, IEnumerable<object> PreCheckNullsValues, Func<MooClass, string> mc )
    {
        if (PreCheckNullsValues.Any(a => a == null))
            Show(t, LabelType, mc(Moo));
        else
            DontShowField(t);
    }

Here's an assumed skeleton to your supporting code:

   enum LabelTypeEnum
    {
        something
    }

    class MooClass
    {
        public FooClass Foo { get; set; }
    }

    class FooClass
    {
        public object DangerousNullRef { get; set; }
        public object AnotherPossibleNullRef { get; set; }
    }

    private void Show(Type TrType, LabelTypeEnum LabelType, string p) { }

    private void DontShowField(Type TrType) { }

You could then use the Action to access your properties safely.

like image 96
deepee1 Avatar answered Mar 19 '26 11:03

deepee1


I wouldn't consider this an improvement, but it can be done using delayed execution through lambda's.

ShowHandlingNull(Moo.Foo, TrType, LabelType, f => f.DangerousNullRef, f => f.AnotherPossibleNullRef);

void ShowHandlingNull(Foo foo, object trType, objectLablelType, Func<Foo, object> dangerousNullRefGetter, Funct<Foo, object> anotherDangerousGetter)
{
    if (foo == null) {
        DontShowField(trType);
        return;
    }
    Show(TrType, LabelType, dangerousNullRefGetter(foo) + " - " + anotherDangerousGetter(foo));
}

But I think your original if null check is easier to understand and maintain.

like image 43
Samuel Neff Avatar answered Mar 19 '26 09:03

Samuel Neff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!