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!
Jon Skeet [C# MVP] - 23 Oct 2007 14:57 GMT
<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
Larry Smith - 23 Oct 2007 15:32 GMT
> 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;
Stoitcho Goutsev (100) - 23 Oct 2007 16:02 GMT
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).
My suggestion is if it is possible to use bit flags. This way you can simply
check just by using bitwise operators. e.g.
enum TaskStates
{
None = 0,
Idle = 1,
Proc1 = 2,
Proc2 = 4,
Proc3 = 8
}
in order to check wehter some value is some set you can do
if((val & (TaskStates.Proc1 |TaskStates.Proc3)) != TaskStates.None)
{
do Something
}
I agree that this is not so descriptive, but you can have predefined values
in the enumerations that can make it for a shorter if statement
enum TaskStates
{
None = 0,
Idle = 1,
Proc1 = 2,
Proc2 = 4,
Proc3 = 8,
UsualTasks = Idle|Proc1|Proc3
}
if((val & TaskStates.UsualTasks) != TaskStates.None)
{
do Something
}
Ofcourse this method is limited by the bit-size of the integer numbers
supported by the platform.
For more complicated cases one should create a class that provides this
functionality, which is not dificult task.
HTH

Signature
Stoitcho Goutsev (100)
> Hello!
>
[quoted text clipped - 39 lines]
>
> Thank you!
Chris Shepherd - 23 Oct 2007 16:41 GMT
> 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.
Jon Skeet [C# MVP] - 23 Oct 2007 17:39 GMT
> > 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.

Signature
Jon Skeet - <skeet@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
Rudi - 25 Oct 2007 17:46 GMT
> 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
Chris Dunaway - 24 Oct 2007 22:36 GMT
> Hello!
>
[quoted text clipped - 40 lines]
>
> Thank you!
Perhaps this Set library may help you:
http://www.codeproject.com/csharp/sets.asp
Chris