1

I have 54,000 files each with a slightly different method of naming and I need to check whether the filename contains a particular string. However, I can't find a ways of getting a Select statement to work correctly.

My code fails unexpectedly. Please can anyone tell me why the string "BAR" is not found in "BARITONE"?


Dim tempCheck as string = "1st BARITONE" Dim GoodOne as Boolean = False Select Case tempCheck Case tempCheck.Contains("CORN") GoodOne = True Case tempCheck.Contains("HORN") GoodOne = True Case tempCheck.Contains("BAR") GoodOne = True Case tempCheck.Contains("TROM") GoodOne = True Case tempCheck.Contains("EUP") GoodOne = True Case Else GoodOne = False End Select 
3
  • 1
    Please don't do it that way ! define a generic list of acceptable strings, then loop through it and as soon as you find a matching value, set a flag to true and get of the loop. That way you don't need to add lines of codes if values are added. It's even better if values aren't hardcoded but in some config file ! Commented Jul 31, 2013 at 13:44
  • @Bartdude Even easier with Linq: things.Any(i => stuff.Contains(i)) (C#, but still). Commented Jul 31, 2013 at 13:48
  • @GrantThomas : unfortunately I haven't been able to use Linq in any of my current projects, but each time I see one of these lines I'm even more eager to try it. Seems to definitely save a LOT of time ! Commented Jul 31, 2013 at 13:52

2 Answers 2

6

Try it like this:

 Dim tempCheck as string = "1st BARITONE" Dim GoodOne as Boolean = False Select Case True Case tempCheck.Contains("CORN") GoodOne = True Case tempCheck.Contains("HORN") GoodOne = True Case tempCheck.Contains("BAR") GoodOne = True Case tempCheck.Contains("TROM") GoodOne = True Case tempCheck.Contains("EUP") GoodOne = True Case Else GoodOne = False End Select 

SELECT CASE doesn't work on strings like this (you have to compare string to a string, in this case you compare to a boolean). It does work on Booleans compared to booleans

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

5 Comments

What would be the advantage of this over a series of If statements?
@CodyGray none; but at least this one works. I personally don't see any special reason for use case statement ever.
@CodyGray not much, just keeping with OP original structure
+1 for giving a solution meeting the expectations of the OP perfectly (even the select case structure).
I don't believe it! Thanks Yuriy.
3

You are using Select Case wrong. Just use a simple if else:

Dim tempCheck As String = "1st BARITONE" Dim GoodOne As Boolean = False If tempCheck.Contains("CORN") Then GoodOne = True ElseIf tempCheck.Contains("HORN") Then GoodOne = True ElseIf tempCheck.Contains("BAR") Then GoodOne = True ElseIf tempCheck.Contains("TROM") Then GoodOne = True ElseIf tempCheck.Contains("EUP") Then GoodOne = True End If 

3 Comments

It would work in VB.NET (even if it's not best practise): Select Case True Case tempCheck.Contains("CORN")
@TimSchmelter I suppose (and the other answer demonstrates that), but honestly you are bending Select Case for something it's not really intended for at that point. I'd prefer my answer.
I don't see any advantage in using If...ElseIf. But I just prefer Select Case - most of the time:)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.