Reputation: 43
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
Upvotes: 1
Views: 3124
Reputation: 39777
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
Upvotes: 6
Reputation: 26078
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
Upvotes: 3