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?
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.
Here’s an assumed skeleton to your supporting code:
You could then use the Action to access your properties safely.