IN Function ?

  • Thread starter Thread starter Jay
  • Start date Start date
J

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.
 
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 said:
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.
 
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.
 
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"
 
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 said:
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
///
 
Jay said:
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.
 
Jay said:
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.
 
Patrice said:
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"
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.
 
Back
Top