Select case wildcard

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

Is it possible to compare a vaule for a select case statement using a
wildcard.

e.g. somthing like

Select case myValue
case like "*ing"
end select

would evaluate as true for "singing", "raining", etc.

If it is possible what is the corerct sysntax (obviously I used the one
above and it didn't work).

Regards

Michael Bond
 
If myValue Like "*ing" Then
....
ElseIf myValue Like "*bla" Then
....
End If

"Like" is a little known and handy VB Operator.
 
If myValue Like "*ing" Then
...
ElseIf myValue Like "*bla" Then
...
End If

For this particular pattern, using String.EndsWith method would be faster:
If myValue.EndsWith("ing") Then
....
ElseIf myValue.EndsWith("bla") Then
....
End If

Of course, you cannot use it with more complicated patterns.
 
Sorry Guys

I know how to use the wildcard in an "if" loop. My question was about
whether, or not, it could be used in a Select Case statment

Regards

Michael Bond
 
I know how to use the wildcard in an "if" loop. My question was about
whether, or not, it could be used in a Select Case statment

You mean "if" block. Not loop. ;-)
I don't think VB has ever supported Like in Select Case comparisons. A
little interesting considering you could do so many other things with Select
Case (like alphabetical comparisons using "To" etc). But, I'd be surprised
if MS changed this behavior in the latest versions of VB. It's Tradition I
guess.

From VB.Classic documentation: Note that Is and Like can't be used as
comparison operators in a Select Case statement.
 
mabond said:
Sorry Guys

I know how to use the wildcard in an "if" loop. My question was about
whether, or not, it could be used in a Select Case statment

Dim s1 As String = "abc"
Dim s2 As String = "ef"

Select Case True
Case s1 Like "a*c"
'...
Case s2.EndsWith("f")
'....
Case s1 & s2 = "abcef"
'...
Case Today.DayOfWeek = DayOfWeek.Friday And Today.Day = 13
'...
Case Else
'...
End Select

Note C# can't do this in a switch statement :) (yes I know if else if
is equivalent...)
 
Back
Top