Home All Groups Group Topic Archive Search About
Author
17 Mar 2006 12:42 PM
mabond
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

Author
17 Mar 2006 12:57 PM
CMM
If myValue Like "*ing" Then
....
ElseIf myValue Like "*bla" Then
....
End If

"Like" is a little known and handy VB Operator.

--
-C. Moya
www.cmoya.com
Show quoteHide quote
"mabond" <mab***@discussions.microsoft.com> wrote in message
news:0ED9D400-599F-4708-9D0A-A189C9212DB7@microsoft.com...
> 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
>
>
Author
17 Mar 2006 1:51 PM
Peter Macej
> 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.

--
Peter Macej
Helixoft - http://www.vbdocman.com
VBdocman - Automatic generator of technical documentation for VB, VB
..NET and ASP .NET code
Author
20 Mar 2006 2:59 PM
mabond
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

Show quoteHide quote
"Peter Macej" wrote:

> > 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.
>
> --
> Peter Macej
> Helixoft - http://www.vbdocman.com
> VBdocman - Automatic generator of technical documentation for VB, VB
> ..NET and ASP .NET code
>
Author
20 Mar 2006 3:53 PM
CMM
> 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.

--
-C. Moya
www.cmoya.com
Show quoteHide quote
"mabond" <mab***@discussions.microsoft.com> wrote in message
news:2EF55CC4-D960-4D8B-B560-A3DFD03EFBC1@microsoft.com...
> 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
>
> "Peter Macej" wrote:
>
>> > 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.
>>
>> --
>> Peter Macej
>> Helixoft - http://www.vbdocman.com
>> VBdocman - Automatic generator of technical documentation for VB, VB
>> ..NET and ASP .NET code
>>
Author
20 Mar 2006 3:55 PM
Larry Lard
mabond wrote:
> 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...)

--
Larry Lard
Replies to group please
Author
20 Mar 2006 5:52 PM
CMM
Very Nice.
(Personally, I think If/ElseIf is more intuitive though.)

--
-C. Moya
www.cmoya.com
Show quoteHide quote
"Larry Lard" <larryl***@hotmail.com> wrote in message
news:1142870154.315843.69510@v46g2000cwv.googlegroups.com...
>
> mabond wrote:
>> 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...)
>
> --
> Larry Lard
> Replies to group please
>