Home All Groups Group Topic Archive Search About
Author
21 Nov 2006 2:33 PM
Jay
What is the quivelant to the SQL IN function in VB.NET?  Eg. I need to know
if a string contains a specific character.

dim varwhatever as string
if varwhatever in ("5","6","9") then ...

I've been using:

if varwhatever="5" or varwhatever="6" or varwhatever="9" then ...

But there must be a better, more effecient way.

Thanks a lot.

Author
21 Nov 2006 2:46 PM
rowe_newsgroups
Instr() will return the position of a character in a string.

i.e.

dim str as string = "569"
dim i as integer = str.instr("6")
' i = 2

Thanks,

Seth Rowe


Jay wrote:
Show quoteHide quote
> What is the quivelant to the SQL IN function in VB.NET?  Eg. I need to know
> if a string contains a specific character.
>
> dim varwhatever as string
> if varwhatever in ("5","6","9") then ...
>
> I've been using:
>
> if varwhatever="5" or varwhatever="6" or varwhatever="9" then ...
>
> But there must be a better, more effecient way.
>
> Thanks a lot.
Author
21 Nov 2006 2:49 PM
Brian Tkatch
Jay wrote:
> What is the quivelant to the SQL IN function in VB.NET?  Eg. I need to know
> if a string contains a specific character.
>
> dim varwhatever as string
> if varwhatever in ("5","6","9") then ...
>
> I've been using:
>
> if varwhatever="5" or varwhatever="6" or varwhatever="9" then ...
>
> But there must be a better, more effecient way.
>
> Thanks a lot.

If INSTR("5,6,9,", varwhatever & ',') > 0 Then ...

B.
Author
21 Nov 2006 3:30 PM
Patrice
My personal preference is to use select case.

If you use the InStr trick be aware to add delimiters both before and after
both strings. For example the code below will find "9," in "5,6,19,20"
--
Patrice

"Brian Tkatch" <Maxwell_Sm***@ThePentagon.com> a écrit dans le message de
news: 1164120563.461024.108***@k70g2000cwa.googlegroups.com...
Show quoteHide quote
> Jay wrote:
>> What is the quivelant to the SQL IN function in VB.NET?  Eg. I need to
>> know
>> if a string contains a specific character.
>>
>> dim varwhatever as string
>> if varwhatever in ("5","6","9") then ...
>>
>> I've been using:
>>
>> if varwhatever="5" or varwhatever="6" or varwhatever="9" then ...
>>
>> But there must be a better, more effecient way.
>>
>> Thanks a lot.
>
> If INSTR("5,6,9,", varwhatever & ',') > 0 Then ...
>
> B.
>
Author
22 Nov 2006 4:55 PM
Brian Tkatch
Patrice wrote:
Show quoteHide quote
> My personal preference is to use select case.
>
> If you use the InStr trick be aware to add delimiters both before and after
> both strings. For example the code below will find "9," in "5,6,19,20"
> --
> Patrice
>
> "Brian Tkatch" <Maxwell_Sm***@ThePentagon.com> a écrit dans le message de
> news: 1164120563.461024.108***@k70g2000cwa.googlegroups.com...
> > Jay wrote:
> >> What is the quivelant to the SQL IN function in VB.NET?  Eg. I need to
> >> know
> >> if a string contains a specific character.
> >>
> >> dim varwhatever as string
> >> if varwhatever in ("5","6","9") then ...
> >>
> >> I've been using:
> >>
> >> if varwhatever="5" or varwhatever="6" or varwhatever="9" then ...
> >>
> >> But there must be a better, more effecient way.
> >>
> >> Thanks a lot.
> >
> > If INSTR("5,6,9,", varwhatever & ',') > 0 Then ...
> >
> > B.
> >

> If you use the InStr trick be aware to add delimiters both before and after
> both strings. For example the code below will find "9," in "5,6,19,20"

True. In my defence, i only say him use one digit. :)

B.
Author
21 Nov 2006 3:19 PM
Tim Patrick
I would use a Generic List.

   Dim checkSet As New Generic.List(Of String)

   checkSet.Add("apple")
   checkSet.Add("banana")
   checkSet.Add("grape")

   If (checkSet.Contains("apple") = True) Then
      ' ... add successful IN code here...
   End If

The next version of Visual Basic ("Orcas" or 9.0) will contain a new feature
called LINQ that provides SQL-like queries within the Visual Basic language.

-----
Tim Patrick - www.timaki.com
Start-to-Finish Visual Basic 2005

Show quoteHide quote
> What is the quivelant to the SQL IN function in VB.NET?  Eg. I need to
> know if a string contains a specific character.
>
> dim varwhatever as string
> if varwhatever in ("5","6","9") then ...
> I've been using:
>
> if varwhatever="5" or varwhatever="6" or varwhatever="9" then ...
>
> But there must be a better, more effecient way.
>
> Thanks a lot.
>
Author
21 Nov 2006 7:17 PM
Kelly Ethridge
Everyone has shown good ideas. I'd thought I'd throw another suggestion
out there. How about using a Generic function?

Private Function IsIn(Of T)(ByVal Value As T, ByVal ParamArray Values()
As T) As Boolean
     For Each Item As T In Values
         If Item.Equals(Value) Then Return True
     Next
     Return False
End Function

Kelly

Jay wrote:
Show quoteHide quote
> What is the quivelant to the SQL IN function in VB.NET?  Eg. I need to
> know if a string contains a specific character.
>
> dim varwhatever as string
> if varwhatever in ("5","6","9") then ...
>
> I've been using:
>
> if varwhatever="5" or varwhatever="6" or varwhatever="9" then ...
>
> But there must be a better, more effecient way.
>
> Thanks a lot.
>
Author
22 Nov 2006 12:06 PM
Oenone
Jay wrote:
> I've been using:
> if varwhatever="5" or varwhatever="6" or varwhatever="9" then ...
> But there must be a better, more effecient way.

\\\
    Select Case varwhatever
        Case "5", "6", "9"
            ...do your thing here
    End Select
///

--

(O)enone
Author
22 Nov 2006 2:27 PM
Phill W.
Jay wrote:
Show quoteHide quote
> What is the quivelant to the SQL IN function in VB.NET?  Eg. I need to
> know if a string contains a specific character.
>
> dim varwhatever as string
> if varwhatever in ("5","6","9") then ...
>
> I've been using:
>
> if varwhatever="5" or varwhatever="6" or varwhatever="9" then ...
>
> But there must be a better, more effecient way.
>
> Thanks a lot.
>
Author
22 Nov 2006 2:30 PM
Phill W.
Jay wrote:
> What is the quivelant to the SQL IN function in VB.NET?  Eg. I need to
> know if a string contains a specific character.
>
> dim varwhatever as string
> if varwhatever in ("5","6","9") then ...

Oops - twitchy Send finger, there ...

If you want to be Stone-Age about it

    If varwhatever like "*[569]*" Then

still works perfectly well.

These days, you'd probably want a Regular Expression, something like
(air-code warning):

Dim mc As MatchCollection _
    = Regex.Matches( varwhatever, "([569])" )
? mc.Groups(0).Text

HTH,
    Phill  W.