Pages Menu
TwitterRssFacebook
Categories Menu

Most recent articles

VB.net If then Else with a list of strings

Posted by on Apr 7, 2015 in Blog, Computer Tips, Programming | 0 comments

Jpeg

In SQL server you can do an easy exclude or include list by using an in statement.
So you can do:

Select * from someTable where somecolumn in (‘somestring1′, ‘somestring2′, ‘somestring3′, ‘somestring4′)

So you only get records where the contents of somecolumn are in the specified list of strings.
I wanted to do something similiar in VB.net.
You can use an if then or statement but it can get messy.

If someStringToCheck = “somestring1″ or someStringToCheck = “somestring2″ or someStringToCheck = “somestring3″ or someStringToCheck = “somestring4″ then
…Do something blah blah…
end if

This works but what if you have a list of 20 strings you want to check for?
Through some internet searching I found this post on stackoverflow.com and came up with the following:

Dim someList = New List(Of String) From {“somestring1″, “somestring2″, “somestring3″, “somestring4″}
If someList.Contains(someStringToCheck) Then
…Do something blah blah…
else
…Do something else blah blah…
end if

It seems kind of backwards but it takes your list of strings and determines if the string you fed it is in that list.  If so do something or if not then do something else.