Chad
Chad

Reputation: 24699

What is the most concise way to handle this Is Nothing/Empty String check?

What is the best way to checkt o see if the TelephoneNumber property has some string in it that does not Trim down to an empty string? I hate to use the Trim VB.NET function on Nothing just to give me an empty string whenever the TelephoneNumber is Nothing.

   Public Class TelephoneNumberType

        Public Property TelephoneNumber() As String

    End Class

  Public Class Form1

    Private _PhoneObj As BusinessObjects.TelephoneNumberType

     Public Sub MySub


        If _PhoneObj IsNot Nothing AndAlso _
           _PhoneObj.TelephoneNumber isNot Nothing AndAlso _
           _PhoneObj.TelephoneNumber.Trim <> String.Empty Then
           MessageBox.Show("I have a non blank phone!")
        End If

      End Sub

    End Class

Edit:

Check out the accepted answer and then call it as follows

If _PhoneObj IsNot Nothing AndAlso _
   _PhoneObj.TelephoneNumber.IsNullOrEmpty Then
    MessageBox.Show("I have a non blank phone!")
End If

What puzzles me is that the "IsNullOrEmpty" function works as an extension method of a string instance even if the String is NOTHING. Why does this extension method work but the follow straight - .NET code raise an exception?

Dim s as string


If s.trim() = "" Then   
    Message.Box.Show("This will never print") 
End If

Upvotes: 1

Views: 2644

Answers (4)

MarkJ
MarkJ

Reputation: 30408

The most concise code is surely

If Trim(str) = "" Then 
  '' Nothing, "" or whitespace 

Works because VB treats Nothing as equivalent to "", and because the Microsoft.VisualBasic function Trim is a function, rather than an instance method, so its happy with Nothing

Upvotes: 1

Code Maverick
Code Maverick

Reputation: 20415

Use a StringExtensions Module like I do. I created this for my projects:

Option Explicit On
Option Strict On

Imports System.Runtime.CompilerServices

Public Module StringExtensions

    <Extension()> _
    Public Function IsNullOrEmpty(ByVal s As String) As Boolean
        Return s Is Nothing OrElse s.Trim.Length.Equals(0)
    End Function

    <Extension()> _
    Public Function IsNotNullOrEmpty(ByVal s As String) As Boolean
        Return s IsNot Nothing AndAlso s.Trim.Length > 0
    End Function

End Module

This allows you to do the following:

Dim s1 As String = Nothing
Dim s2 As String = String.Empty
Dim s3 As String = "123"

If s1.IsNullOrEmpty Then
    'Do Something
End If 

If s2.IsNullOrEmpty Then
    'Do Something
End If

If s3.IsNotNullOrEmpty Then
    'Do Something
End If

All are so simple to use.

EDIT:

Here's a great helper extension I made for a need of mine that I'll throw in for ya:

    <Extension()> _
    Public Function ContainsAny(ByVal s As String, ByVal ParamArray values As String()) As Boolean
        If s.IsNotNullOrEmpty AndAlso values.Length > 0 Then
            For Each value As String In values
                If s.Contains(value) Then Return True
            Next
        End If

        Return False
    End Function

Upvotes: 1

ZeNo
ZeNo

Reputation: 1658

if(!string.IsNullorEmpty(str) && str.Trim() != string.Empty)
//non empty string
else
//emptry string

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564851

You can replace this with a call to String.IsNullOrWhiteSpace (requires .NET 4):

If _PhoneObj IsNot Nothing AndAlso Not String.IsNullOrWhitespace(_PhoneObj.TelephoneNumber) Then
   MessageBox.Show("I have a non blank phone!")
End If

Upvotes: 6

Related Questions