473,461 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Has if () syntax of C# no IN operator like Pascal or SQL (IN, LIKE,BETWEEN)?

Hello!

I'm searching like a similar syntax for if():

int i=5;
if (i in [1,3,5..10]) doSomething;

e.g.

enum TaskStates {Idle,Proc1,Proc2, ... ProcN}
static TaskStates taskState = TaskStates.Idle;

if (taskState in
[TaskStates.Idle,TaskStates.Proc1,TaskStates.Proc2, TaskStates.Proc5 [..]
TaskStates.Proc10]) doSomething;

Could it really to be the truth, that there is no such construct in c#?
All books and online searches were not helpfully.

I wont write neither this

if ((taskState == TaskStates.Idle) || (taskState == TaskStates.Proc1) ||
((taskState >= TaskStates.Proc5) && (taskState <= TaskStates.Proc10))
doSomething;

nor that:

switch(taskState)
{
case TaskStates.Idle:
case TaskStates.Proc1:
case TaskStates.Proc2:
case TaskStates.Proc5:
case TaskStates.Proc6:
case TaskStates.Proc7:
case TaskStates.Proc8:
case TaskStates.Proc9:
case TaskStates.Proc10:
DoSomthing;
break;
}

Thank you!
Oct 23 '07 #1
6 11846
On Oct 23, 2:34 pm, Rudi <rate...@gmx.netwrote:

<snip>
Could it really to be the truth, that there is no such construct in c#?
Yup.
All books and online searches were not helpfully.

I wont write neither this
<snip>
>
nor that:
<snip>

Well, if you won't use either of the constructs which *are* available
in C#, you're going to find it relatively difficult to achieve what
you want.

You can write your own range/set testing classes, of course.

Jon

Oct 23 '07 #2
Hello!
>
I'm searching like a similar syntax for if():

int i=5;
if (i in [1,3,5..10]) doSomething;
You should heed Jon's advice. Moreover, there are many ways of elegantly
searching collections even if they're not always as concise as a simple
keyword. In fact, even this technique can be used:

if (Array.IndexOf(new int[] { 1, 3, 5, 10 }, i) != -1) doSomething;

It may not be as clean but most collections aren't hardcoded on the fly like
this. They're either declared as separate variables with hardcoded values
(making the above even cleaner), or far more likely, they're populated from
some data source. Each collection then has its own particular search
functions which are syntactically easy to use. It usually boils down to
something like the following (for an index returning search) but other
search types are also available (returning the object itself usually or
typically null if not found)

if (yourCollection.Find(whatever) != -1) doSomething;
Oct 23 '07 #3
Stoitcho Goutsev (100) wrote:
Rudi,

Yes this is the case - C# doesn't have an *in* operator. As a matter of fact
neither does C nor C++ (I don't know about Java, but I doubt it).
Well, technically it does, but not for what the OP is looking for.

ie: foreach (string s in listOfStrings)

To my recollection, no, Java does not either.

Chris.
Oct 23 '07 #4
Chris Shepherd <ch**@nospam.chsh.cawrote:
Yes this is the case - C# doesn't have an *in* operator. As a matter of fact
neither does C nor C++ (I don't know about Java, but I doubt it).

Well, technically it does, but not for what the OP is looking for.

ie: foreach (string s in listOfStrings)
If we're going to be pedantic - "in" isn't an operator there. It's a
keyword, but not an operator. :)
To my recollection, no, Java does not either.
Indeed.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 23 '07 #5
On Oct 23, 8:34 am, Rudi <rate...@gmx.netwrote:
Hello!

I'm searching like a similar syntax for if():

int i=5;
if (i in [1,3,5..10]) doSomething;

e.g.

enum TaskStates {Idle,Proc1,Proc2, ... ProcN}
static TaskStates taskState = TaskStates.Idle;

if (taskState in
[TaskStates.Idle,TaskStates.Proc1,TaskStates.Proc2, TaskStates.Proc5 [..]
TaskStates.Proc10]) doSomething;

Could it really to be the truth, that there is no such construct in c#?
All books and online searches were not helpfully.

I wont write neither this

if ((taskState == TaskStates.Idle) || (taskState == TaskStates.Proc1) ||
((taskState >= TaskStates.Proc5) && (taskState <= TaskStates.Proc10))
doSomething;

nor that:

switch(taskState)
{
case TaskStates.Idle:
case TaskStates.Proc1:
case TaskStates.Proc2:
case TaskStates.Proc5:
case TaskStates.Proc6:
case TaskStates.Proc7:
case TaskStates.Proc8:
case TaskStates.Proc9:
case TaskStates.Proc10:
DoSomthing;
break;

}

Thank you!
Perhaps this Set library may help you:

http://www.codeproject.com/csharp/sets.asp

Chris

Oct 24 '07 #6
Stoitcho Goutsev (100) wrote:
My suggestion is if it is possible to use bit flags. This way you can simply
check just by using bitwise operators. e.g.
Thanks, now I got some good solutions and approaches. Nevertheless
hoping the future in C# brings an elegant and simple way for this.

@all

Thank you for help and solutions!

best regards, Rudi
Oct 25 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Hannes Stillerich | last post by:
Hi all, i am looking for a proper xml syntax to describe different processes. a process contains several steps. a step is mostly a call of a software component ((wrapped)COM or .NET assembly,...
9
by: Rich Herrick | last post by:
The latest version of my "Pascal-like" set class is available here: http://www.richherrick.com/software/herrick-1.0.zip Those from the old YAHOO BOOST forum might remember it from several...
25
by: bearophileHUGS | last post by:
This post comes from a boring morning, if you are busy ignore this. This post is only for relaxed people. I've found this page, "Syntax Across Languages", it contains many errors and omissions,...
4
by: Regnab | last post by:
I've got a number of 'Lots' of cattle - ie 40A, 372A, W123 etc. The letter in the Lot number indicates where the animal is traceable or not. The animal's Lot number, along with its individual...
2
by: gwlemyre | last post by:
I am not a PASCAL programmer and I saw this piece of code. procedure Expression; Forward; procedure Factor; begin if Look = '(' then begin Match('('); Expression; Match(')'); end
2
by: swingingming | last post by:
Hi, When I am playing around with options, I came across with enabling "SQL server compatible syntax". After I enable this, I could not get one of my query to run. Can anyone explain the subtle...
11
by: YZXIA | last post by:
Is there any difference between explicit inner join and implicit inner join Example of an explicit inner join: SELECT * FROM employee INNER JOIN department ON employee.DepartmentID =...
9
by: lawpoop | last post by:
I'm trying to write some simple code, but I might be simplifying it too much. I have a function that either returns a string that I want, or FALSE. In the code that I'm working on, I would like...
0
by: Olivier J | last post by:
This is actually an answer to an old post which is closed. But it may nevertheless be useful to someone. Old Post: Has if () syntax of C# no IN operator like Pascal or SQL (IN, LIKE,BETWEEN)? ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.