Click here to Skip to main content
12,623,295 members (29,640 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

13.4K views
13 bookmarked
Posted

A SQL-Like IN function for c# and VB.NET

, 13 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Simplify complex if-statements with "where value in list"

Introduction

This Tip presents helper functions similar to the SQL IN clause for use within .NET programs. The intention is to help avoid combining && /|| (And / Or) within complex if statements

Background

Some considerable time ago I found myself having to write some fairly complex if-statements combining lots of Ands with a whole load of Ors . I very quickly found myself wishing that VB6 (I know, I know!) had something similar to the T-SQL construct IN so I could get rid of the "Ors" and simplify the logic.

So I wrote myself some helper functions to do just that. It really wasn't pretty, and fortunately it has now been condemned to the annals of history (although it is probably still sitting on a backup server somewhere!).

Then recently I came across a post in Quick Answers where the OP had a very similar problem . Here's a (very much) simplified example of what it looked like...

if (!aBoolTest && (aTest == "++" || aTest == "--" || aTest == "**" || 
                aTest == "^/" || aTest == "/>" || aTest == "/<" || 
                aTest == "</=" || aTest == ">/=" || aTest == "/=") && aDoubleTest == .75)

Wouldn't it be nice if that could be written as something like...

if(!aBoolTest && aTest.IN("++","--",^/","/>","/<","</=",">

Using the code

Well, in the first attempt at this article I went in completely the wrong direction. See the comments prior to 13 May 2014 for far better solutions than my original. I guess my mind was still caught up thinking of my old VB6 solution!

However the solution I was really after is even simpler in the context I'm using it ... to replace all those Ors. You just need an array of the values you are interested in - I've created the array on-the-fly within the if-statement itself - and the thing you're testing for. Then utilise the Contains method of the array. All you have to do is ensure that the array you create is the same type as the object you are testing against.

C# example:

if (new string[] {"++", "--", "**", "^/", "/>", "/<", "/=", "/="}.Contains(aTest) )
    Debug.Print("Passed");  // Actions when test passes
else
    Debug.Print("Failed");  // Actions when test fails

VB.NET example:

If (New Char() {"+", "-", "*", "^", "/", "<", "=", ">"}).Contains(aCharTest) Then
    Debug.Print("Passed")   'Actions when test passes
Else
    Debug.Print("Failed")   'Actions when test fails
End If

Points of Interest

I did actually learn something while I was putting together this Tip. VB.NET is not my language of choice but I thought it would make sense to include the VB version here. I got a bit frustrated trying to create a static class in VB before realising that I should have been using a Module to get that effect. Of course, now I'm not using either.

But now I have another list of things to study further - namely extension methods and generics. So I got something out of this first time around, and will gain even more over the next few days courtesy of the members that commented to version 1.

To be honest, this still probably isn't the most original of ideas, and I still haven't seen anything like this documented. Still happy to stand corrected if I'm wrong.

History

Version #1 - 12 May 2014.

Version #2 - 13 May 2014 - Changed approach to in-line array. Dropped the classes altogether

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

CHill60
Software Developer (Senior)
United Kingdom United Kingdom
Works as a Contract Developer mainly in the Financial Services sector.

"Abandon all hope of ever being understood, all ye who have a good idea but don't say it perfectly." Doug Bernard

You may also be interested in...

Pro

Comments and Discussions

 
QuestionA bit amusing Pin
Jörgen Andersson6-Jun-15 0:00
protectorJörgen Andersson6-Jun-15 0:00 
AnswerRe: A bit amusing Pin
CHill607-Jun-15 14:26
protectorCHill607-Jun-15 14:26 
GeneralRe: A bit amusing Pin
Jörgen Andersson7-Jun-15 22:05
protectorJörgen Andersson7-Jun-15 22:05 
GeneralRe: A bit amusing Pin
CHill608-Jun-15 22:20
protectorCHill608-Jun-15 22:20 
QuestionUSE this function from the VFPtoolkit Pin
William Dole20-Jan-15 7:49
memberWilliam Dole20-Jan-15 7:49 
AnswerRe: USE this function from the VFPtoolkit Pin
CHill6020-Jan-15 14:34
protectorCHill6020-Jan-15 14:34 
QuestionWhat about: Pin
sapatag13-May-14 23:11
membersapatag13-May-14 23:11 
AnswerRe: What about: Pin
CHill(Hawkeye)6014-May-14 1:34
protectorCHill(Hawkeye)6014-May-14 1:34 
GeneralRe: What about: Pin
sapatag14-May-14 2:00
membersapatag14-May-14 2:00 
GeneralRe: What about: Pin
CHill(Hawkeye)6014-May-14 3:08
protectorCHill(Hawkeye)6014-May-14 3:08 
QuestionNice! Pin
Volynsky Alex12-May-14 9:09
professionalVolynsky Alex12-May-14 9:09 
AnswerRe: Nice! Pin
CHill(Hawkeye)6012-May-14 10:47
protectorCHill(Hawkeye)6012-May-14 10:47 
GeneralRe: Nice! Pin
Volynsky Alex12-May-14 11:47
professionalVolynsky Alex12-May-14 11:47 
QuestionNot bad Pin
Chris Copeland12-May-14 3:29
memberChris Copeland12-May-14 3:29 
AnswerRe: Not bad Pin
CHill(Hawkeye)6012-May-14 3:31
protectorCHill(Hawkeye)6012-May-14 3:31 
AnswerRe: Not bad Pin
johannesnestler12-May-14 3:33
memberjohannesnestler12-May-14 3:33 
GeneralRe: Not bad Pin
Chris Copeland12-May-14 4:14
memberChris Copeland12-May-14 4:14 
GeneralRe: Not bad Pin
johannesnestler12-May-14 4:28
memberjohannesnestler12-May-14 4:28 
QuestionComplete nonsense Pin
johannesnestler12-May-14 3:28
memberjohannesnestler12-May-14 3:28 
AnswerRe: Complete nonsense Pin
CHill(Hawkeye)6012-May-14 3:31
protectorCHill(Hawkeye)6012-May-14 3:31 
GeneralRe: Complete nonsense Pin
johannesnestler12-May-14 3:42
memberjohannesnestler12-May-14 3:42 
GeneralRe: Complete nonsense Pin
CHill(Hawkeye)6012-May-14 4:36
protectorCHill(Hawkeye)6012-May-14 4:36 
SuggestionNice Pin
Goor12-May-14 3:00
groupGoor12-May-14 3:00 
GeneralRe: Nice Pin
CHill(Hawkeye)6012-May-14 3:29
protectorCHill(Hawkeye)6012-May-14 3:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161128.1 | Last Updated 13 May 2014
Article Copyright 2014 by CHill60
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid