mardi 15 décembre 2015

Tidy way to check for Null (Nothing) Variables in nested classes

I would like to take some action if the Boolean value FormFoo.Bar.Baz.Quux exists and is True. However, Bar and Baz are members of class type and might be Nothing (in which case, the action should not be taken).

I currently have the following code but it is ugly:

If (Not FormFoo.Bar Is Nothing) AndAlso (Not FormFoo.Bar.Baz Is Nothing) AndAlso FormFoo.Bar.Baz.Quux Then
    DoSomething()
End If

Is there an alternative way to do this test that is more readable?

Aucun commentaire:

Enregistrer un commentaire