Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shortening IF Statement

Status
Not open for further replies.

jmikow

Programmer
Mar 27, 2003
114
US
I'm wondering if there is an equivalent in VB.Net for the SQL IN statement.

I currently have an IF statement that looks like

Code:
            If UseCode = "010" Or UseCode = "020" Or UseCode = "021" Or UseCode = "027" Or UseCode = "040" Or _
                UseCode = "050" Or UseCode = "051" Or UseCode = "052" Or UseCode = "053" Or UseCode = "054" Or _
                UseCode = "055" Or UseCode = "056" Or UseCode = "057" Or UseCode = "058" Or UseCode = "059" Or _
                UseCode = "110" Or UseCode = "300" Or UseCode = "410" Or UseCode = "420" Or UseCode = "430" Or _
                UseCode = "450" Then

            End If

Is it possible to reduce this down to something like
Code:
            If UseCode IN ("010","020","021","027","040","050","051","052","053","054","055","056","057","058","059","110","300","410","420","430","450") Then

            End If

Any ideas or suggestions are welcome.

Thanks,

Josh
 
First off use OrElse to short circuit ORELSE every OR expression is tested i.e. no quitting when it finds an equal.

Second, the IndexOf method is very handy.
UseCodes() as string = {"010","020", "021", etc}

if UserCodes.Indexof(UserCode) > -1 then

end if

- free online Compare/Diff of snippets
 
And if the list does not change, use Shared for performance.
Private Shared UserCodes() as string = InitSortedStrings("010","020", "021", etc)

.....
if Array.BinarySearch(UserCodes,UserCode) > -1 then
.....
End if


Private Shared IntitStrings(ParamArray s() as string) as string()
dim a() as string = s
Array.Sort(a)
return a
End Function




- free online Compare/Diff of snippets
 
Or maybe change that to
Private Shared IntitStrings(ParamArray s() as string) as string()
dim a(s.length-1) as string
a = s.Clone ' COPY array before sorting
Array.Sort(a)
return a
End Function


- free online Compare/Diff of snippets
 
Yet another option would be a case statement.
Code:
Select Case UserCode
    Case "010", "020", etc.
End Select

Select Case CInt(UserCode)
    Case 10, 20, etc
End Select
Numeric compasisons may be faster and put your most expected values first to they match more quickly

Have a great day!

j2consulting@yahoo.com
 
Thanks for all of the suggestions.

I'm going to work with them and see which will work the best.

Thanks,

Josh
 
Or maybe

Code:
Dim UseCodes As String = _
"010,020,021,027,040,050,051,052,053,054,055,056,057,058,059,110,300,410,420,430,450" 

' You can use string builder discussed in many other threads to build the UseCodes value

If Instr(Usecodes,UseCode,CompareMethod.Text) Then
   ' Codes here
End If

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top