I'm looking for a 'safe' dot notation in VB.net. Does such a thing exist - in VB.NET or any language? What I want to be able to do is, when using non-nullable legacy objects, to solve a problem like:
"if there as a plan, if there is a case, if it has a person, that person's spouse, else, nothing (VBSpeak for Null)."
and avoid doing 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 this:
Dim Spouse as Person = Case~Plan~Person~Spouse
Where '~'
is my sought 'safe' dot notation, which immeditely returns a null upon encountering the first null object instead of throwing an exception?
More common for this problem of course:
dim MyVar as string = XMLDoc.DocumentElement.SelectSingleNode("Name").InnerText
and wanting Nothing instead of an exception when Name doesn't exist.
Edit:
Is there a way to solve this problem using LINQ for objects or LINQ for XML?