I am aware that similar questions have been asked before:
But my issue is not addressed there. In C# I can write this:
return myFoo ??= new Foo()
If myFoo
is null, this line will create a new Foo
, assign it to myFoo
, and return the value of myFoo
.
I want to translate this to VB. I can do it in two statements e.g.:
If myFoo Is Nothing Then myFoo = New Foo()
Return myFoo
or even using the two-argument If
function:
myFoo = If(myFoo, New Foo())
Return myFoo
I would prefer to do it in one statement. This looks like it ought to be equivalent to the C# version:
Return myFoo = If(myFoo, New Foo())
But it doesn't work because it interprets the =
as a comparator, not an assignment.
Is there an elegant solution?