Null Safe Spot recording in VB.NET ... or does it exist in any language? "safe dereference operator" or equivalent using LINQ?

I am looking for "safe" dot notation in VB.net. Is there such a thing - in VB.NET or in any language? I want to be able to use non-nullable deprecated objects to solve the problem, for example:

"if there is a plan, if there is a case, if he has a person, this is the person of the spouse, otherwise nothing (VBSpeak for Null)."

and do not do this:

Dim Spouse as Person = Nothing
if Case.Plan isnot nothing then
    if Case.Plan.Person isnot Nothing
        Spouse = Case.Plan.Person.Spouse
    end if
end if

and do the following:

Dim Spouse as Person = Case~Plan~Person~Spouse

Where '~'is my sought-after “safe” dot notation that immediately returns zero when it collides with the first null object instead of throwing an exception?

Most often for this problem:

dim MyVar as string = XMLDoc.DocumentElement.SelectSingleNode("Name").InnerText

Nothing , Name .

:

, LINQ LINQ XML?

+3
4

VB.NET 14 null-conditional . .

Dim Spouse as Person = Case?.Plan?.Person?.Spouse

null (?.) (? [). , .

+2

, , AakashM. LENGTH. LENGTH , , .

LEN , null, 0 (, -, 0, .)

, IsNotNull , , IsValid. "" (, , )

Lambdas, , . , , IsNotNull null, Proxy, , , , . -, .

+1

VB 14 (Visual Studio 2015) .

, ifs, AndAlso (short-circuiting And):

If Case.Plan IsNot Nothing _
AndAlso Case.Plan.Person IsNot Nothing _
AndAlso Case.Plan.Person.Name = "Something" Then
    'Do something here
End If

Null Object Pattern. .

Edit:

LINQ, . LINQ - (, ..). LINQ, , .

+1

VB.NET, , #?? . Iif() If, , .

, . , - , , , - . . NullReferenceException, . , , . , NotAplan NoSpouse, , " , ". , , + . bool.

+1

All Articles