jeudi 27 août 2015

Checking whether nested/hierarchy of properties are null?

I have:

label.Text = myObject.myNestedObject.MyNestedObject2.Description;

Where label is asp.net label. The problem is that sometimes myObject, myNestedObject, MyNestedObject2 or Description is null and I must check it in if statement which looks like:

if(myObject!=null&&myNestedObject!=null&&MyNestedObject2!=null&&Description!=null)
{
label.Text = myObject.myNestedObject.MyNestedObject2.Description;
}

In this statement I check four times whether properties is null. Does exist any other way to check entire hierarchy?

Aucun commentaire:

Enregistrer un commentaire