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.
rowe_newsgroups - 21 Nov 2006 14:46 GMT
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
> What is the quivelant to the SQL IN function in VB.NET? Eg. I need to know
> if a string contains a specific character.
[quoted text clipped - 9 lines]
>
> Thanks a lot.
Brian Tkatch - 21 Nov 2006 14:49 GMT
> What is the quivelant to the SQL IN function in VB.NET? Eg. I need to know
> if a string contains a specific character.
[quoted text clipped - 9 lines]
>
> Thanks a lot.
If INSTR("5,6,9,", varwhatever & ',') > 0 Then ...
B.
Patrice - 21 Nov 2006 15:30 GMT
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"

Signature
Patrice
>> What is the quivelant to the SQL IN function in VB.NET? Eg. I need to
>> know
[quoted text clipped - 14 lines]
>
> B.
Brian Tkatch - 22 Nov 2006 16:55 GMT
> My personal preference is to use select case.
>
[quoted text clipped - 21 lines]
> >
> > 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.
Tim Patrick - 21 Nov 2006 15:19 GMT
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
> What is the quivelant to the SQL IN function in VB.NET? Eg. I need to
> know if a string contains a specific character.
[quoted text clipped - 8 lines]
>
> Thanks a lot.
Kelly Ethridge - 21 Nov 2006 19:17 GMT
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
> What is the quivelant to the SQL IN function in VB.NET? Eg. I need to
> know if a string contains a specific character.
[quoted text clipped - 9 lines]
>
> Thanks a lot.
Oenone - 22 Nov 2006 12:06 GMT
> 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
///

Signature
(O)enone
Phill W. - 22 Nov 2006 14:27 GMT
> What is the quivelant to the SQL IN function in VB.NET? Eg. I need to
> know if a string contains a specific character.
[quoted text clipped - 9 lines]
>
> Thanks a lot.
Phill W. - 22 Nov 2006 14:30 GMT
> 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.