4

I have an If statement that checks a number of things (BaySlots() is an array of strings).

If (Not BaySlots.Contains(Not String.Empty)) OrElse
        (Not BayAcId = 0 AndAlso Not BayAcId = acProgramId _
            AndAlso overrideSetting = False) Then

I though the Array.Contains method of the first condition would be sufficient to tell me if the array held only empty strings, but it gives InvalidCastException: Conversion from string "" to type Long is not valid so I'm guessing Not String.Empty is actually evaluated to something that is attempted to be converted to a Long.

Is there a better method I can use to retro-fit this If, so that I can still include the test for only empty strings in the array, as part of the If, rather than having to add a preceding loop to test each index of BaySlots() for an empty string?

I thought there should probably be some way of testing this other than looping as that would be a relative lot of work just to test if there was no content.

Thanks

PS just to clarify this is not to test if the array has zero dimensions or is equal to Nothing but that the strings it does contain are all equal to String.Empty.

3
  • Do you want it return true if all of the elements in the array are Nothing, or you only want it to return true if they are all non-empty string objects? Commented Sep 10, 2013 at 11:58
  • I'd like the entire first condition: Not BaySlots.Contains(Not String.Empty)) to evaluate to true. Its basically saying "If there are no strings which are not empty" or equally "If all strings are empty" Commented Sep 10, 2013 at 12:10
  • 1
    In that case, it looks like Heizi's second solution, using All will do exactly what you want. Commented Sep 10, 2013 at 12:12

1 Answer 1

4

LINQ's Enumerable.Any can do this. A direct translation of your Not Contains(Not String.Empty))would be:

If (Not BaySlots.Any(Function(x) x <> "")) OrElse ...

(Feel free to replace "" with String.Empty, if that's what you prefer.)


Since you have a double negation here, I suggest to replace it with Enumerable.All for readability:

If BaySlots.All(Function(x) x = "") OrElse ...

This also communicates your intent more clearly ("If all entries are empty...").


Note: In VB.NET, comparing a string to "" or String.Empty also yields True if the string is Nothing.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! I'd been trying to get rid of the double-neg as it was but hadn't managed it yet by myself :) +1 for the links to the interface methods too!
Note that you could also use If BaySlots.All(AddressOf String.IsNullOrEmpty) ...
why not use .IndexOfAny(String.Empty) or .IndexOf(String.Empty) im sure these will also return position(s) of empty indices

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.