Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Dear experts,
 
This will only take you 1 min max. I need to make a multiple conditional OR statement in a if statement. I know I can or them seperately, but is there a shortcut. This is my failed attempt:
 
if (value == (6 || 8 || 10))
{
    size1 = value;
}
Posted 19-Jan-13 3:35am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Here's another way:
 
private enum MySet
{
    Six = 6
,
    Eight = 8
,
    Ten = 10
}
 
if ( System.Enum.IsDefined ( typeof(MySet) , i ) )
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 20-Jan-13 0:17am
   
Ha! Like this one, a 5.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
I found this:
if ((value | 6 | 8 | 10) == (6 | 8 | 10) && value != 0)
{
    size1 = value;
}
In this case, there's no many difference in length of your condition. If you've variables with long names, or if many options are possible, this is a shorter condition.
 
Alternatively, you can write an extension method[^] for objects:
public static class MultipleOrConditionClass
{
    public static bool MultipleOrCondition(this object obj, params object[] objectsToCompare)
    {
        return objectsToCompare.Contains(obj);
    }
}
Now, use this condition:
  if (value.MultipleOrCondition(6, 8, 10))
            {
                size1 = value;
            }
Hope this helps.
  Permalink  
v2
Comments
Gilmore Sacco at 20-Jan-13 8:06am
   
Thank you very muxh, programFOX, it worked like a charm. I do have another issue, if you have the time to help me. I would really appriceate it: http://www.codeproject.com/Questions/531057/CheckplusForplusnullplusvalueplusinplusCSVplusstri
ProgramFOX at 20-Jan-13 8:50am
   
Thank you very muxh
You're welcome!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Use switch statement
 
switch (value)
{
   case 6:
   case 8:
   case 10:
     size1 = value;
  break;
}
 
or if you really want to do this create extension method.
 
static class Extensions
{
    public static bool In<T>(this T value, params T[] values)
    {
        if (values == null)
            throw new ArgumentNullException("values");
 
        return values.Contains(value);
    }
}
  if(value.In(6,8,10)){
    size1 = value;
  }
  Permalink  
v2
Comments
Tharaka MTR at 19-Jan-13 21:36pm
   
again someone down-voted without checking the complete answer ... :p
PIEBALDconsult at 20-Jan-13 8:09am
   
The original version wasn't nearly as good.
Tharaka MTR at 20-Jan-13 8:13am
   
Ok. I am agreed. first I posted only SWITCH statement. But Within a minute of time I posted the extension method. If you downvoted just seen the SWITCH statement, I'm agreed with you. My bad. sorry...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

No.
 
But you could use a HashSet
 
if ( new System.Collections.Generic.HashSet ( new int[] { 6 , 8 , 10 } ).Contains ( value ) )
 
Though if you did that you should make the HashSet a static readonly field.
 

I also wonder what you want to do when the value isn't in the set?
  Permalink  
Comments
Gilmore Sacco at 19-Jan-13 10:26am
   
if it is not in set, i am setting as zero

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



Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 19 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid