Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If Not String.Empty ignoring empty string - VB.NET

I have a array of strings and I am looping through them, but the string might be empty so I am trying this:

For Each Component As String In Components
    If Component IsNot String.Empty Then
        'Work your magic
    End If
Next

But if Component is an empty string the logic still fires. I've also tried

If Component <> "" Then 

End If

With the same results. So what am I missing?

like image 602
personaelit Avatar asked Aug 11 '10 17:08

personaelit


1 Answers

  1. Make sure that your List is of type string
  2. Use the String.IsNullOrEmpty method.

    Sub Main
        Dim foo As String
        foo = "Non-Empty string"
        If Not String.IsNullOrEmpty(foo) Then
            Console.WriteLine("Foo is not empty.")
        End If
    End Sub
    
like image 98
Daniel May Avatar answered Sep 28 '22 08:09

Daniel May