5

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?

1
  • Just adding for completeness: your assumption that Component is empty string must have been false. It was either a non-empty string, or Nothing. Commented Aug 11, 2010 at 19:33

3 Answers 3

25
  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
    
Sign up to request clarification or add additional context in comments.

Comments

1

One thing that has gotten me before is spaces. You can't see it when you view the variable in the watch window, but it makes the string not empty or null.

1 Comment

That wasn't it, but +1 for the tip.
0

Do your string have default values and are they actually ""? What if you used:

If Not Component Is Nothing Then

End If

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.