ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

NewIfNull

Shortcut to create an object if it's null

Source

<Extension>
Public Function NewIfNull(Of T As Class)(ByRef value As T) As T

    If value Is Nothing Then
        value = GetType(T).CreateInstance
        Return value
    Else
        Return value
    End If

End Function

Example

Private _person As Person

Public ReadOnly Property Person As Person
   Get
        Return _person.NewIfNull
   End Get
End Property

Author: Simone Ferrari

Submitted on: 4 feb. 2017

Language: VB

Type: System.Object

Views: 3709