|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Select case wildcardIs 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. 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 > > > If myValue Like "*ing" Then For this particular pattern, using String.EndsWith method would be faster:> ... > ElseIf myValue Like "*bla" Then > ... > End If 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 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 > > I know how to use the wildcard in an "if" loop. My question was about You mean "if" block. Not loop. ;-)> whether, or not, it could be used in a Select Case statment 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. 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 >> mabond wrote:
> Sorry Guys Dim s1 As String = "abc"> > 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 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 Very Nice.
(Personally, I think If/ElseIf is more intuitive though.) 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 > |
|||||||||||||||||||||||