Empty Object ToString

I bet most people aware that direct convert an empty object using ToString() will throw exception. A typical way to overcome this is to check if the object is empty, return an emptry string instead. Easily done on C#, today was trying to achive the same with vb.

Private Function ToSafeString(ByVal obj As Object) As String
        If obj Is Nothing Then
            Return String.Empty
            Return obj.ToString()
        End If
    End Function

Being so use with C# “null”, found out in vb is “Nothing”.

1 thought on “Empty Object ToString”

Leave a Reply

Your email address will not be published. Required fields are marked *